In this tutorial, we will learn how to use the Iterator interface to iterate over collections such as List, Set, and Map with examples.
Learn Java collections framework at https://www.javaguides.net/p/java-collections-tutorial.html
Iterator Interface Overview
- In Java, Iterator is an interface available in the Collection framework in java.util package.
- Java Iterator interface used to iterate over the elements in a collection (list, set or map). It helps to retrieve the specified collection elements one by one and perform operations over each element.
- It is available since Java 1.2 Collection Framework.
- It supports both READ and REMOVE Operations.
- Compare to the Enumeration interface, Iterator method names are simple and easy to use.
Iterator Interface Methods
- default void forEachRemaining(Consumer<? super E> action) - This method is used to perform the given action for each remaining element until all elements have been processed or the action throws an exception.
- boolean hasNext() - This method returns true if the iteration has more elements.
- E next() - This method returns the next element in the iteration.
- default void remove() - This method removes from the underlying collection the last element returned by this iterator (optional operation).
List (ArrayList) Iterator Example
Here is the example to iterate over an ArrayList using Iterator interface:
package com.java.tutorials.collections;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Iterator Examples
* @author Ramesh Fadatare
*
*/
public class IteratorExample {
public static void main(String[] args) {
List < String > fruits = new ArrayList < String > ();
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Mango");
fruits.add("Orange");
Iterator < String > iterator = fruits.iterator();
while (iterator.hasNext()) {
String fruit = (String) iterator.next();
System.out.println(fruit);
if ("Banana".equals(fruit)) {
iterator.remove();
}
}
System.out.println(fruits);
}
}
Output:
Banana
Apple
Mango
Orange
[Apple, Mango, Orange]
Set (HashSet) Iterator Example
Here is the example to iterate over a HashSet using Iterator interface:
package com.java.tutorials.collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Iterator Examples
* @author Ramesh Fadatare
*
*/
public class IteratorExample {
public static void main(String[] args) {
Set < String > fruits = new HashSet < String > ();
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Mango");
fruits.add("Orange");
Iterator < String > iterator = fruits.iterator();
while (iterator.hasNext()) {
String fruit = (String) iterator.next();
if ("Apple".equals(fruit)) {
iterator.remove();
}
}
System.out.println(fruits);
}
}
Output:
[Mango, Orange, Banana]
HashMap Keys Iterator Example
Here is the Java example to iterate over keys of a HashMap:
package com.java.tutorials.collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Iterator Examples
* @author Ramesh Fadatare
*
*/
public class IteratorExample {
public static void main(String[] args) {
Map < String, String > map = new HashMap < String, String > ();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
Iterator < String > iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println(key);
}
}
}
Output:
key1
key2
key3
key4
HashMap Values Iterator Example
Here is the Java example to iterate over values of a HashMap:
package com.java.tutorials.collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Iterator Examples
* @author Ramesh Fadatare
*
*/
public class IteratorExample {
public static void main(String[] args) {
Map < String, String > map = new HashMap < String, String > ();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
Iterator < String > iterator = map.values().iterator();
while (iterator.hasNext()) {
String value = (String) iterator.next();
System.out.println(value);
}
}
}
Output:
value1
value2
value3
value4
References
- https://www.javaguides.net/p/java-collections-tutorial.html
- https://www.javaguides.net/2018/06/guide-to-hashmap-class.html
- https://www.javaguides.net/2018/06/guide-to-hashset-class.html
- https://www.javaguides.net/2018/06/guide-to-hashset-class.html
Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course