Java Iterator interface belongs to java.util package and it is a part of Collection framework. Iterator interface allows us to iterate over collection of different class objects such as ArrayList, HashSet etc.
Iterator supports add and remove operations only.
Iterator<T> iterator;
hasNext() → returns true if iterator has more elements.
next() → returns next element if iterator has more elements.
remove() → remove the element from iterator. There is no return type of this method.
remove method throws two below exceptions:
Below is the example to iterate through ArrayList.
Note: I always try to make things easy and simple, please read comments while reading code snippet will help to understand better.
Remove() method will help us to remove element from list.
Custom iterator will help us to iterate over custom collection class.
Follow below steps to create custom iterator:
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved