- Using iterator –
import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class LinkedListExample { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add("ram"); list.add("mohan"); list.add("sohan"); list.add("shyam"); list.add("ram"); Iterator<String> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
Output is –
ram
mohan
sohan
shyam
ram
2. Using ListIterator –
import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class LinkedListExample { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add("ram"); list.add("mohan"); list.add("sohan"); list.add("shyam"); list.add("ram"); ListIterator<String> listIte = list.listIterator(); while (listIte.hasNext()) { System.out.println(listIte.next()); } } }
Output is –
ram
mohan
sohan
shyam
ram
3. Using For-each loop –
import java.util.LinkedList; import java.util.List; public class LinkedListExample { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add("ram"); list.add("mohan"); list.add("sohan"); list.add("shyam"); list.add("ram"); for (String str : list) { System.out.println(str); } } }
Output is –
ram
mohan
sohan
shyam
ram
4. Using for loop –
import java.util.LinkedList; import java.util.List; public class LinkedListExample { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add("ram"); list.add("mohan"); list.add("sohan"); list.add("shyam"); list.add("ram"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
Output is –
ram
mohan
sohan
shyam
ram