Reputation: 11
I was studying framework and I saw an example which is transferring from HashMap to Set like Set set = map.entrySet();
then Iterator it = set.iterator();
for using Iterator method for access each key set.
From that, I'm confused that is Iterator method only for Set in Collection framework?
Upvotes: 1
Views: 45
Reputation: 1132
Iterator<> is an interface, as is Collection<>.
An implementation of Collection is required to be iterable. It will provide an implementation of Iterator that is specialized to iterate over that collection type, and a method to return an instance of that iterator.
So, if I implement class PileOfStuff implements Collection<Stuff>
, somewhere behind the scenes I'll have something like class PileOfStuffIterator implements Iterator<Stuff>
and PileOfStuff.iterator()
will return such an iterator.
Upvotes: 1