EvictingBoundedList implements Iterable<T>, Serializable { public T set(int index, T element) { Preconditions.checkArgument(index >= 0 && index < count); ++modCount; if (isDroppedIndex(index)) { return getDefaultElement(); } else { int idx = index % elements.length; T old = accessInternal(idx); elements[idx] = element; return old; } } EvictingBoundedList(int sizeLimit);  EvictingBoundedList(EvictingBoundedList<T> other);  EvictingBoundedList(int sizeLimit, T defaultElement); int size(); boolean isEmpty(); boolean add(T t); void clear(); T get(int index); int getSizeLimit(); T set(int index, T element); T getDefaultElement(); @Override Iterator<T> iterator(); EvictingBoundedList<R> map(Function<T, R> transform);  }
@Test public void testSet() { int insertSize = 17; int boundSize = 5; Integer defaultElement = 4711; List<Integer> reference = new ArrayList<>(insertSize); EvictingBoundedList<Integer> list = new EvictingBoundedList<>(boundSize, defaultElement); for (int i = 0; i < insertSize; ++i) { reference.add(i); list.add(i); } assertEquals(reference.size(), list.size()); list.set(0, 123); list.set(insertSize - boundSize - 1, 123); list.set(insertSize - boundSize, 42); reference.set(insertSize - boundSize, 42); list.set(13, 43); reference.set(13, 43); list.set(16, 44); reference.set(16, 44); try { list.set(insertSize, 23); fail("Illegal index in set not detected."); } catch (IllegalArgumentException ignored) { } for (int i = 0; i < insertSize; ++i) { int exp = i < (insertSize - boundSize) ? defaultElement : reference.get(i); int act = list.get(i); assertEquals(exp, act); } assertEquals(reference.size(), list.size()); }