Both Iterator and Iterable are interfaces in Java Collection Framework. Here are some differences:
Iterator: Iterator is an interface that manages iteration over an Iterable. It maintains a state of where we are in the current iteration, and knows what the next element is and how to get it. See API here
Iterator lets you check if it has more elements using hasNext() and move to the next element (if any) using next().
This interface is a member of the Java Collections Framework. It has the methods hasNext(), next(), remove()
Iterable: Implementing this interface allows an object to be the target of the "for-each loop" statement. See Iterable API here.
An iterable produces iterators
Iterator: Iterator is an interface that manages iteration over an Iterable. It maintains a state of where we are in the current iteration, and knows what the next element is and how to get it. See API here
Iterator lets you check if it has more elements using hasNext() and move to the next element (if any) using next().
This interface is a member of the Java Collections Framework. It has the methods hasNext(), next(), remove()
Iterable: Implementing this interface allows an object to be the target of the "for-each loop" statement. See Iterable API here.
An iterable produces iterators
0 comments:
Post a Comment