hasNext(): It returns true if the iteration has more elements.
next(): It returns the next element in the iteration.
Example:
package com.w3schools;
import java.util.Iterator;
import java.util.LinkedList;
public class Test {
public static void main(String args[]){
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Jai");
linkedList.add("Mahesh");
linkedList.add("Naren");
linkedList.add("Vivek");
linkedList.add("Vishal");
linkedList.add("Hemant");
Iterator<String> iterator = linkedList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
} |
package com.w3schools;
import java.util.Iterator;
import java.util.LinkedList;
public class Test {
public static void main(String args[]){
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Jai");
linkedList.add("Mahesh");
linkedList.add("Naren");
linkedList.add("Vivek");
linkedList.add("Vishal");
linkedList.add("Hemant");
Iterator<String> iterator = linkedList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Output
Jai
Mahesh
Naren
Vivek
Vishal
Hemant |
Jai
Mahesh
Naren
Vivek
Vishal
Hemant