In this program, different styles of retrieving the elements and also extracting a few elements from the list are shown.
Example on LinkedList Iterators Get Elements Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.*; public class LinkedListIterators { public static void main(String args[]) { LinkedList<String> myList = new LinkedList<String>(); myList.add("one"); myList.add("two"); myList.add("six"); myList.add("zero"); myList.add("nine"); // USING FOREACH System.out.print("Elements with foreach: "); for(String str : myList) { System.out.print(str + " "); } // USING ITERATOR System.out.print("\nElements with Iterator: "); Iterator it1 = myList.iterator(); while(it1.hasNext()) { System.out.print(it1.next() + " "); } // USING ADDALL LinkedList<String> yourList = new LinkedList<String>(); yourList.addAll(myList); // USING LISTITERATOR System.out.print("\nElements with ListIterator: "); ListIterator it2 = yourList.listIterator(2); while(it2.hasNext()) { System.out.print(it2.next() + " "); } // USING SUBLIST List<String> ourList = myList.subList(1,5); System.out.print("\n\nSublist elements: " + ourList); ourList.clear(); System.out.println("\nourList size after calling clear(): " + ourList.size()); } } |

Output screenshot on LinkedList Iterators Get Elements Java
1 2 |
LinkedList<String> myList = new LinkedList<String>(); myList.add("one"); |
A generics linked list object myList is created that stores strings only. With add() method, inherited from Collection interface, a few strings are added.
1 2 3 4 |
for(String str : myList) { System.out.print(str + " "); } |
The above for loop is a modified form of basic for loop (generally in other languages known as foreach) and was introduced with JDK 1.5. The working of this enhanced for loop is discussed very clearly in “Collections Enhanced for loop (foreach)“.
1 2 3 4 5 |
Iterator it1 = myList.iterator(); while(it1.hasNext()) { System.out.print(it1.next() + " "); } |
The Iterator (equivalent to Enumeration) interface is used to print the elements of the linked list. The methods hasNext() and next() are illustrated elaborately in “Iterator Interface“.
1 2 |
LinkedList<String> yourList = new LinkedList<String>(); yourList.addAll(myList); |
Another generics linked list object yourList is created without any elements. All the elements of myList are added at a time with addAll() method inherited from Collection interface.
1 2 3 4 5 |
ListIterator it2 = yourList.listIterator(2); while(it2.hasNext()) { System.out.print(it2.next() + " "); } |
Using ListIterator object it2 all the elements of the linked list from 2nd element are printed. The ListIterator is capable to print in reverse order and is shown in “ListIterator Interface“.
The comparison of Iterator and ListIterator is shown in “Iterator vs ListIterator“.
1 2 |
List<String> ourList = myList.subList(1,5); ourList.clear(); |
subList() method, inherited from List interface, is used to extract a few elements (1, 2, 3 and 4) of myList and are made another list ourList. Like this, from one list, we can make a number of lists with our choicest elements.