Java Collection Tutorial - Java Iterator.hasNext()








Syntax

Iterator.hasNext() has the following syntax.

boolean hasNext()

Example

In the following code shows how to use Iterator.hasNext() method.

//from www. j  a va 2  s. c om
import java.util.Iterator;
import java.util.LinkedList;

public class Main{
    public static void main(String args[]) {
        LinkedList<String> ll = new LinkedList<String>();

        ll.add("A");
        ll.add("ja v a2s.com");
        ll.addLast("B");
        ll.add("C");

        Iterator<String> itr = ll.iterator();
        while (itr.hasNext()) {
            String element = itr.next();
            System.out.print(element + " ");
        }
    }
}
  

The output: