List Example Java

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

Four programs are given on List with different functionalities.

  1. List Methods: Illustrates frequently used methods like size(), add(), get(), contains(), indexOf(), lastIndexOf(), subList(), remove(), clear() and isEmpty().
  2. List Iteration: Illustrates 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: Illustrates reversing and sorting the list elements with methods asList(), sort(), reverseorder.
  4. List Unique Elements: Illustrates removing duplicate elements from list, sorting list elements, converting list to Set and again set to list.

This first program on List illustrates the most frequently used methods like size(), add(), get(), contains(), indexOf(), lastIndexOf(), subList(), remove(), clear() and isEmpty().

Example on List Example Java
import java.util.*;
public class ListDemo1
  public static void main(String[] args) 
    List cities = new ArrayList();
    System.out.println("\nElements exist before adding elements: " + cities.isEmpty());
    cities.add("Hyderabad");	   cities.add("Chennai");
    cities.add("Bengaluru");             cities.add("Kochi");
    System.out.println("Elements exist after adding elements: " + cities.isEmpty());
    System.out.println("No. of cities: " + cities.size());

    System.out.println("cities contains Chennai: " + cities.contains("Chennai"));
    System.out.println("City at index number 3: " + cities.get(3));

    cities.add("Pune");	   cities.add("Mumbai");  cities.add("Chennai");
    System.out.println("\nNew size of cities after adding three elements: " + cities.size());
    System.out.println("Elements of cities after adding 3 more: \n" + cities);

    System.out.println("\nIndex of Chennai: " + cities.indexOf("Chennai"));
    System.out.println("Last Index of Chennai: " + cities.lastIndexOf("Chennai"));
    List subCities = cities.subList(2,5);
    System.out.println("Elements of subCities: " + subCities);

    System.out.println("\nElements of subCities after two removes: " + subCities);
    System.out.println("Elements of cities after two removes: " + cities);

    System.out.println("\nSize of subCities after calling clear(): " + subCities.size());    
    System.out.println("Elements of subCities after calling clear(): " + subCities);

List Example Java
Output screen on List Example Java

List cities = new ArrayList();
System.out.println(“\nElements exist before adding elements: ” + cities.isEmpty());

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, cities can be treated as an object of List.

System.out.println(“Elements exist after adding elements: ” + cities.isEmpty());
System.out.println(“No. of cities: ” + cities.size());

With add() method, inherited by Collection interface, elements are added to list cities. isEmpty() method returns true if no elements exist with the list. The above statement returns true as still elements are not added. The size() method returns the number of elements present in the list. The elements added are given index numbers by default, the first element index being 0.

System.out.println(“cities contains Chennai: ” + cities.contains(“Chennai”));
System.out.println(“City at index number 3: ” + cities.get(3));

contains("Chennai") returns a boolean value of true if the element Chennai exists with list cities.

System.out.println(“\nIndex of Chennai: ” + cities.indexOf(“Chennai”));
System.out.println(“Last Index of Chennai: ” + cities.lastIndexOf(“Chennai”));

As the list allows duplicates (remember, Set does not allow), the same element can be added a number of times. In the list cities, Chennai is added two times at different index numbers. indexOf("Chennai") method returns the first occurrence of Chennai and lastIndexOf("Chennai") returns the last occurrence of Chennai in the list.

List subCities = cities.subList(2,5);

The method subList(2, 5) returns another list, subCities, containing the elements 2, 3 and 4. Like this, a few elements can be extracted and made as another list.


The remove() method is overloaded that takes index number as parameter and also element name.

Note: Removing from sub list also removes from the original list.


The clear() method deletes all the elements.

Leave a Comment

Your email address will not be published.