Using Iterators: Java Collections Framework Video Tutorial, Part 10

A look at using iterators in Java; how "for each" loops work and how to add or remove elements from a list while iterating through it.

After starting the video, click the maximise button to make it fullscreen so you can see the code!



Code For This Tutorial



App.java:

 

import java.util.Iterator;
import java.util.LinkedList;

public class App {

    public static void main(String[] args) {

        LinkedList<String> animals = new LinkedList<String>();

        animals.add("fox");
        animals.add("cat");
        animals.add("dog");
        animals.add("rabbit");
        
        // "Old" way of iterating through lists (except that generics
        // didn't exist pre Java 5). This way is still an integral part
        // of Java; it allows you to remove items from the list
        // and also supports the "for each" syntax behind the scenes.

        Iterator<String> it = animals.iterator();

        while (it.hasNext()) {
            String value = it.next();
            System.out.println(value);
            
            if(value.equals("cat")) {
                it.remove();
            }
        }

        System.out.println();
        
        /*
         * If you want to add items to a list while iterating through
         * it, get a ListIterator using the .listIterator() method.
         * ListIterator also has a previous() method, allowing you to
         * "rewind" the iterator so that you can add items before
         * the current item.
         */

        // / Modern iteration, Java 5 and later; "for each" loop

        for (String animal : animals) {
            System.out.println(animal);
            
            // The following won't work; you need an iterator.
            // Throws ConcurrentModificationException
            // animals.remove(2);
        }
    }

}


 
fox
cat
dog
rabbit

fox
dog
rabbit