-
-
Notifications
You must be signed in to change notification settings - Fork 653
Open
Labels
Description
Traversable interface is extended by several other interfaces of collections, one of those is Iterator.
But it behaves differently than the others when using head() and tail() methods, see here below the test that fails for Iterator, but not for all other sequential collections.
class ScratchTest {
public static void main(String[] args) {
testTraversable(LinkedHashSet.of(0, 1, 2, 3));
testTraversable(List.of(0, 1, 2, 3));
testTraversable(Queue.of(0, 1, 2, 3));
testTraversable(Array.of(0, 1, 2, 3));
testTraversable(Vector.of(0, 1, 2, 3));
testTraversable(Iterator.of(0, 1, 2, 3)); // fails!
}
private static void testTraversable(Traversable<Integer> traversable) {
assertEquals(Integer.valueOf(0), traversable.head());
traversable = traversable.tail();
assertEquals(Integer.valueOf(1), traversable.head());
traversable = traversable.tail();
assertEquals(Integer.valueOf(2), traversable.head());
traversable = traversable.tail();
assertEquals(Integer.valueOf(3), traversable.head());
}
}