I am new in Java and I'm really confused with iterator and iterable. Can anyone explain to me and give some examples?
Java – Difference Between Iterator and Iterable
iterableiteratorjava
iterableiteratorjava
I am new in Java and I'm really confused with iterator and iterable. Can anyone explain to me and give some examples?
Best Answer
An
Iterable
is a simple representation of a series of elements that can be iterated over. It does not have any iteration state such as a "current element". Instead, it has one method that produces anIterator
.An
Iterator
is the object with iteration state. It lets you check if it has more elements usinghasNext()
and move to the next element (if any) usingnext()
.Typically, an
Iterable
should be able to produce any number of validIterator
s.