An iterable is a collection of objects that can be traversed. The objects are traversed using an iterator that uses particular methods to iterate over an object.
Iterator<Integer> iterator =
Arrays.asList(1,2,3).iterator();
iterator = iter(1,2,3)
An iterator uses the following methods to traverse objects in Java:
hasNext()
next()
forEachRemaining()
(this can only be used in Java 8)An iterator uses the following methods to traverse objects in Python:
next()
: returns the next item in the iterable.iter()
: returns the iterator object itself.The codes below explain the relationship between the iterable and iterator: