Explain traversing through a collector using Iterator.
Ans. We can access each element in Collection by using Iterators regardless of how they are organized in collector. Iterator can be implemented a different way for each Collection. To use an iterator to traverse through the contents of a collection we do:
- Obtain an iterator by calling collections iterator()method to the start of collection.
- Set up a loop which makes a call to hasNext(). Have the loop iterate as long as hasNext()returns true.
- Within loop, obtain every element by calling next().
- remove() method is used to remove current element in iteration.