List Tutorial Java


List Tutorial Java: It is advised to read List Fundamentals before proceeding the following programs.

Four programs are given on List with different functionalities.

  1. List Methods: Uses frequently used methods like size(), add(), get(), contains(), indexOf(), lastIndexOf(), subList(), remove(), clear() and isEmpty().
  2. List Iteration: Comparing two lists with equals(), converting list elements to array with toArray(), printing array elements with foreach loop, printing list elements with Iterator (forward) and ListIterator (forward and backward).
  3. List Reversing Sorting: Reversing and sorting the list elements with methods asList(), sort(), reverseorder.
  4. List Unique Elements: Removing duplicate elements from list, sorting list elements, converting list to Set and again set to list.

This is the second program on List Tutorial Java and does the following operations on list elements.

  1. Comparing two lists with equals()
  2. Converting list elements to array with toArray()
  3. Printing array elements with foreach loop
  4. Printing list elements with Iterator (forward)
  5. Printing list elements with ListIterator (forward and backward).
import java.util.*;
public class ListDemo3
{
  public static void main(String args[])
  {
    List myList = new ArrayList();
    myList.add("Gandhiji");     myList.add("Nehruji");     myList.add("Sastryji");

    List yourList = new ArrayList();
    yourList.add("Gandhiji");     yourList.add("Nehruji");     yourList.add("Sastryji");
                                                   // TO CHECK TWO LISTS EQUAL
    if(myList.equals(yourList))
    {
      System.out.println("Both lists are same");
    }
                                                //  LIST TO ARRAY
    Object leaders[] = myList.toArray();
    System.out.print("Array elements with foreach: ");
                                       // ARRAY ELEMENTS WITH FOREACH
    for(Object obj : leaders)
    {
      System.out.print(obj + " ");
    }
                                         // LIST ELEMENTS WITH ITERATOR
    System.out.print("\n\nElements with Iterator: ");
    Iterator it1 = myList.iterator();
    while(it1.hasNext())
    {
      System.out.print(it1.next() + " ");
    }
                                         // LIST ELEMENTS WITH LISTITERATOR
    System.out.print("\n\nElements with ListIterator forward: ");
    ListIterator it2 = myList.listIterator();
    while(it2.hasNext())
    {
      System.out.print(it2.next() + " ");
    }

    System.out.print("\nElements with ListIterator backward: ");
    while(it2.hasPrevious())
    {
      System.out.print(it2.previous() + " ");
    }
  }
}


List Tutorial Java
Output screenshot on List Tutorial Java

       List myList = new ArrayList();

List, being an interface, cannot be instantiated, but its reference can be assigned by assigning its subclass object. We know a subclass object can be assigned to a super class object implicitly. ArrayList is derived from List interface. For all practical purposes, myList and yourList can be treated as objects of List.

       if(myList.equals(yourList))
       {
         System.out.println("Both lists are same");
       }

Lists myList and yourList are added same elements. equals() method can be used to compare two lists. If they contain same elements, the method returns true else false.

Note: If a list contains another list as an element, equals() does not work properly.

       Object leaders[] = myList.toArray();

In the second program, List Reversing Sorting, we have seen how to convert a string array into list. Now the above statement, converts the other way, list elements into an array.

       for(Object obj : leaders)
       {
         System.out.print(obj + " ");
       }

With enhanced for loop (known as foreach loop), all the elements of the array are printed.

       Iterator it1 = myList.iterator();
       while(it1.hasNext())
       {
         System.out.print(it1.next() + " ");
       }

The iterator() method of list, the elements are printed.

         ListIterator it2 = myList.listIterator();
         while(it2.hasNext())
         {
           System.out.print(it2.next() + " ");
         }

listIterator() method of List returns an object ListIterator interface. ListIterator is derived from Iterator and added more features of traversing the elements in forward and backward direction. For forward iteration, it uses hasNext() and next() methods.

        while(it2.hasPrevious())
        {
          System.out.print(it2.previous() + " ");
        }

For backward iteration, hasPrevious() and previous() are used. See the screenshot.

Leave a Comment

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