How iterate LinkedList by using iterator

import java.util.Iterator;

import java.util.LinkedList;

public class LinklistIterator {

public static void main(String[] args) {

LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add(“Mumbai”);
linkedList.add(“Delhi”);
linkedList.add(“Noida”);
linkedList.add(“Gao”);
linkedList.add(“Patna”);

Iterator<String>  itr = linkedList.iterator();
while (itr.hasNext()) {
System.out.println(“Element is =”+itr.next());

}

}

}

Output:

Element is =Mumbai

Element is =Delhi

Element is =Noida

Element is =Gao

Element is =Patna

 

Leave a Reply

Your email address will not be published. Required fields are marked *

64 − = 56