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

Guava provides an Iterables.cycle(), coupled with a Multiset for counting and 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"));
        for (int i = 0; i < 7; i++) {
            System.out.println(circular.getOne());
        }
        System.out.println("Count for A: " + circular.getCount("A"));
    }
} 

Output:

A
B
C
A
B
C
A
Count for A: 3

NB: Beware to have proper equals/hashCode for type T


the above answer is correct, just adding one more simpler way of round robin algorithm with list

import java.util.Iterator;
import java.util.List;

public class MyRoundRobin<T> implements Iterable<T> {
      private List<T> coll;
      private int index = 0;

      public MyRoundRobin(List<T> coll) { this.coll = coll; }

      public Iterator<T> iterator() { 
         return new Iterator<T>() {


            @Override
            public boolean hasNext() {
                return true;
            }

            @Override
            public T next() {
                index++;
                if(index>=coll.size()) {
                    index = 0;
                }
                T res = coll.get(index);
                return res;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }

        };
    }
}