๐ŸŽ“ A1Learner
๐Ÿ”
0 of 48 lessons completed

Java Iterator

An Iterator is an object that walks through a collection one item at a time. It can also remove items during the walk.

Walk a list with hasNext and next

The while loop runs while hasNext is true. Each next call returns one name.

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> names = new ArrayList<String>();
    names.add("Ada");
    names.add("Ben");
    names.add("Cid");
    Iterator<String> it = names.iterator();
    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }
}

Remove the even numbers during a walk

The walk visits each number once. remove drops each even number, and the survivors print after the walk.

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(1);
    numbers.add(2);
    numbers.add(3);
    numbers.add(4);
    numbers.add(5);
    numbers.add(6);
    Iterator<Integer> it = numbers.iterator();
    while (it.hasNext()) {
      int number = it.next();
      if (number % 2 == 0) {
        it.remove();
      }
    }
    for (int number : numbers) {
      System.out.println(number);
    }
  }
}

Remove one named item during a walk

The walk compares each name with Ben. The matching item is removed, and the other names print after the walk.

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> names = new ArrayList<String>();
    names.add("Ada");
    names.add("Ben");
    names.add("Cid");
    Iterator<String> it = names.iterator();
    while (it.hasNext()) {
      String name = it.next();
      if (name.equals("Ben")) {
        it.remove();
      }
    }
    System.out.println(names.size());
    for (String name : names) {
      System.out.println(name);
    }
  }
}

Example

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> scores = new ArrayList<Integer>();
    scores.add(45);
    scores.add(80);
    scores.add(30);
    scores.add(90);
    Iterator<Integer> it = scores.iterator();
    while (it.hasNext()) {
      int score = it.next();
      if (score < 50) {
        it.remove();
      }
    }
    System.out.println(scores.size());
    for (int score : scores) {
      System.out.println(score);
    }
  }
}

Key points

  • Iterator lives in java.util and is imported before use.
  • The iterator method on a collection returns an Iterator.
  • hasNext reports whether another item remains.
  • next returns the next item and moves the walk forward.
  • remove deletes the item that next returned last.
  • Removal during a walk goes through the iterator itself.

In this example, the iterator walks four scores. Scores below 50 are removed during the walk. Two scores survive, so the size prints 2. The loop then prints the survivors, 80 and 90.

Note

An iterator walks in one direction only, from the start to the end.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which Iterator method deletes the item that next returned last?