NestedIterator implements Iterator<T> { @Override public T next() { if (this.innerIterator == null) { throw new NoSuchElementException(); } T result = this.innerIterator.next(); if (!this.innerIterator.hasNext()) { advanceOuterIterator(); } return result; } NestedIterator(Iterable<? extends Iterable<T>> iterableOfIterables); @Override boolean hasNext(); @Override T next(); @Override void remove();  }
@Test(expected = NoSuchElementException.class) public void iterateBeyondInnerList() { NestedIterator<String> nestedIterator = new NestedIterator<>( Collections.singletonList(Collections.emptyList())); nestedIterator.next(); }
@Test(expected = NoSuchElementException.class) public void iterateBeyondOuterList() { NestedIterator<String> nestedIterator = new NestedIterator<>( Collections.emptyList()); nestedIterator.next(); }