Java Iterator Tutorial with Examples

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


Comments