Java Iterator.hasNext()

Syntax

Iterator.hasNext() has the following syntax.

boolean hasNext()

Example

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


//ww w . j  a  va2s .  c o m
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: