How do I retrieve LinkedList objects using Iterator?

This example show you how to use the Iterator interface to retrieve objects from a LinkedList.

package org.kodejava.example.util;

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

public class LinkedListIterator {
    public static void main(String[] args) {
        List grades = new LinkedList();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        //
        // Retrieve objects in LinkedList using Iterator.
        //
        Iterator iterator = grades.iterator();
        while (iterator.hasNext()) {
            System.out.println("Grade: " + iterator.next());
        }
    }
}

This program prints the following output:

Grade: A
Grade: B
Grade: C
Grade: D
Grade: E

Wayan Saryada

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply