LinkedList Iterators Get Elements Java


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
import java.util.*;
public class LinkedListIterators
{
  public static void main(String args[])
  {
    LinkedList myList = new LinkedList();
    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 yourList = new LinkedList();
    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 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

       LinkedList myList = new LinkedList();
       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.

       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)“.

        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“.

       LinkedList yourList = new LinkedList();
       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.

       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“.

           List 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.

Leave a Comment

Your email address will not be published.