WindowIterator implements Iterator<List<T>> { @Override public List<T> next() { window.removeFirst(); window.add(iterator.next()); return new LinkedList<>(window); } WindowIterator(Iterator<T> iterator, int windowSize); @Override boolean hasNext(); @Override List<T> next(); @Override void remove();  }
@Test public void testNextReturnsIterables() { int windowSize = 3; Iterable<Object> iterable = Arrays.<Object>asList(0, 1, 2, 3, 4, 5); WindowIterator<Object> windowIterator = new WindowIterator<>(iterable.iterator(), windowSize); assertThat(windowIterator.next(), CoreMatchers.<Object>hasItems(0, 1, 2)); assertThat(windowIterator.next(), CoreMatchers.<Object>hasItems(1, 2, 3)); assertThat(windowIterator.next(), CoreMatchers.<Object>hasItems(2, 3, 4)); assertThat(windowIterator.next(), CoreMatchers.<Object>hasItems(3, 4, 5)); }