iterator - Java - Implementing a round-robin circular List, and counting element's access count? -


scenario:


for list have 3 elements [a, b, c]:

you can circular access many times want. , there additional counting function records access count of each element.

for example, if accessing 7 times, should return:

      [a, b, c, a, b, c, a]  

and have access count of each element following:

      +–––––––––––+–––––––––––––––+     |  element  |  access count |     +–––––––––––––––––––––––––––+     |         |       3       |     +–––––––––––––––––––––––––––+     |     b     |       2       |     +–––––––––––––––––––––––––––+     |     c     |       2       |     +–––––––––––+–––––––––––––––+  

any response appreciated.

regards.


updated


add additional function allow caller specify elements list should filtered. still use 7 times accessing example, filtering [c]:

      [a, b, a, b, a, b, a]  
      +–––––––––––+–––––––––––––––+     |  element  |  access count |     +–––––––––––––––––––––––––––+     |         |       4       |     +–––––––––––––––––––––––––––+     |     b     |       3       |     +–––––––––––––––––––––––––––+     |     c     |       0       |     +–––––––––––+–––––––––––––––+  

and, subsequent calling on getnextone() should fetch 1 access count low (simulate load-balanced accessing count implementation.). so, if second caller attempt accessing 10 times, should return:

      [c, c, c, b, c, a, b, c, a, b, c, a]  
      +–––––––––––+–––––––––––––––+     |  element  |  access count |     +–––––––––––––––––––––––––––+     |         |       7       |     +–––––––––––––––––––––––––––+     |     b     |       6       |     +–––––––––––––––––––––––––––+     |     c     |       6       |     +–––––––––––+–––––––––––––––+  

guava provides iterables.cycle(), coupled multiset counting , you're done:

package com.stackoverflow.so22869350;  import com.google.common.collect.hashmultiset; import com.google.common.collect.iterables; import com.google.common.collect.lists; import com.google.common.collect.multiset;  import java.util.iterator; import java.util.list;  public class circular<t> {      private final multiset<t> counter;      private final iterator<t> elements;      public circular(final list<t> elements) {         this.counter = hashmultiset.create();         this.elements = iterables.cycle(elements).iterator();     }      public t getone() {         final t element = this.elements.next();         this.counter.add(element);         return element;     }      public int getcount(final t element) {         return this.counter.count(element);     }      public static void main(final string[] args) {         final circular<string> circular = new circular<>(lists.newarraylist("a", "b", "c"));         (int = 0; < 7; i++) {             system.out.println(circular.getone());         }         system.out.println("count a: " + circular.getcount("a"));     } }  

output:

a b c b c count a: 3 

nb: beware have proper equals/hashcode type t


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -