You know, there are several ways to iterate collections in Java, but using iterator is the most basic and original. And any Java programmers should be familiar with and fluent in using iterator.
Table of content:1. Why using Iterator?

List<String> listFruits = List.of("Apple", "Banana", "Lemon", "Dragon Fruit", "Durian");The following code will iterate over all elements and print each, using a while loop:Iterator<String> iterator = listFruits.iterator();
while (iterator.hasNext()) {
	String aFruit = iterator.next();
	System.out.println(aFruit);
}It’s also possible to use a for loop with an iterator, as shown below:for ( ; iterator.hasNext(); ) {
	String aFruit = iterator.next();
	System.out.println(aFruit);
}If the list is mutable, we can remove elements from it. For example, the following code will remove String elements starting with “D”:while (iterator.hasNext()) {
	String aFruit = iterator.next();
		
	if (aFruit.startsWith("D")) iterator.remove();
}If the list is immutable, you will get UnsupportedOperationException.Set<Integer> numbers = new HashSet<>(Set.of(1, 2, 3, 4, 5, 6, 7, 8));
Iterator<Integer> iterator = numbers.iterator();
while (iterator.hasNext()) {
	Integer nextNumber = iterator.next();
	System.out.println(nextNumber);
}This example creates a set with some fixed elements, and use iterator to go through all elements, and print out value of each number in the set. Note that Sets do not order elements so there’s no guarantee about the order of elements returned by the iterator.Map<Integer, String> map = Map.of(200, "OK", 404, "Not Found", 500, "Internal Server Error");
Iterator<Integer> keyIterator = map.keySet().iterator();
while (keyIterator.hasNext()) {
	Integer nextKey = keyIterator.next();
	String nextValue = map.get(nextKey);
	
	System.out.println(nextKey + " => " + nextValue);
}Here, the keyIterator object is of a Set collection that contains keys in the map. It is not iterator of the Map itself.Queue<String> queue = new PriorityQueue<>(Arrays.asList("One", "Two", "Three", "Four"));
Iterator<String> iterator = queue.iterator();
while (iterator.hasNext()) {
	String next = iterator.next();
	
	System.out.println(next);
} void process(Collection<String> collection) {
	Iterator<String> iterator = collection.iterator();
	
	while (iterator.hasNext()) {
		String next = iterator.next();
		
		// process the next element returned by the iterator
		System.out.println(next);
	}
}You see, this method uses a parameter of type Collection, which means it can accept List, Set or Queue. NOTES: The Iterator interface defines the forEachRemaining() method which can be used to iterate all elements in a collection, as shown the following code:List<String> listFruits = List.of("Apple", "Banana", "Lemon", "Dragon Fruit", "Durian");
listFruits.iterator().forEachRemaining(System.out::println);This method takes a parameter of type Consumer, which means you can pass a Lambda expression that results an object implementing that functional interface.I hope you find this article helpful, in terms of understanding how to use iterator in the Java Collections framework. Watch the following video to see the coding of Java iterator in action:  Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.