Java list – A Collection Interface

A visual illustration explaining the Java List interface. It point outs main features of lists, its implementations with example.

List interface in Java

The list is a sub-interface of Collection in Java.

This is an ordered Collection that may contain duplicate elements.

The Collection in Java is the group of objects.

The objects are known as elements that can be ordered and unordered.

The collection has a few sub-interfaces and List is one of those.

One other popular sub-interface is the Set. The set does not allow duplicate entries.

List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
List<String> vector = new Vector<>();

Important points about Java List

  • The List inherits the operation from Collection.
  • Additionally, the List interface includes positional access, search, iteration, and range-view operations.
  • The elements in the List can be added, removed, and searched.
  • The index of list elements starts at 0.
  • The List interface is implemented in different classes. The popular are ArrayList, LinkedList, Vector, etc.
  • Being an interface, the List object cannot be initialized.

See the following section for implementing the List interface in ArrayList, LinkedList, and Vector classes. I will also show you a few examples of using the List methods for different operations.

An example of List Java with ArrayList class

This example shows how you may create a List object with ArrayList class.

After creating an object, the elements in the List are added by using the add method of the List. Finally, a for loop is used for displaying the elements in that List object:

The code for creating and displaying the List:

import java.util.*;

public class dist_demo {

    public static void main(String []args) {

        List<Integer> aList_demo = new ArrayList<Integer> (5);

        aList_demo.add(5);
        aList_demo.add(10);
        aList_demo.add(15);
        aList_demo.add(20);
        aList_demo.add(25);

        //Displaying List
        for (int i=0;i<aList_demo.size();i++){

                 System.out.println("The List element: " +aList_demo.get(i));
              }
 }
}

Output:

The List element: 5
The List element: 10
The List element: 15The List element: 20
The List element: 25

An example of using LinkedList class

In this example, the Java LinkedList class is used with the List interface for creating an object.

Just like the above example, the elements are added by using the add method. However, the list is declared as a string type. So, it may contain only string elements.

The code:

import java.util.*;

public class dist_demo {
    public static void main(String []args) {

                List<String> list_linkedList = new LinkedList<String>();

                list_linkedList.add("The");
                list_linkedList.add("Java");
                list_linkedList.add("List");
                list_linkedList.add("Tutorial");

        System.out.println("The List with LinkedList class:");

        System.out.print("\n");
        System.out.print(list_linkedList);

 }

}

Output:

The List with LinkedList class:
[The, Java, List, Tutorial]

A demo of using the Vector class

The Vector class is among those classes that implement the List in Java. See this example where a list object is created with a Vector class of string type.

The same method i.e. add() is used for elements to be added to the list.

A for..each loop is used for displaying the list elements:

The code for creating a list with Vector class:

import java.util.*;

public class dist_demo {

    public static void main(String []args) {

                List<String> list_vector = new Vector<String>();

                list_vector.add("The");
                list_vector.add("List");
                list_vector.add("with");
                list_vector.add("Vector");
                list_vector.add("Class");

                 System.out.println("The elements in list with Vector class:");
                 System.out.println("\n");

                  for (String currVectorElement : list_vector) {
 
                   System.out.println(currVectorElement);

               }
 }
}

Output:

The elements in list with Vector class:
The
List
with
Vector
Class

An example of removing an element from the list

The remove() method can be used for deleting the specific element from the list. You may provide the index of the element or object in the remove() method.

In this example, a list is created by using the ArrayList class. Five elements are added to the list by using the add method. After that, remove method is used for deleting 3rd index element. Have a look:

The code for removing the list element by index:

import java.util.*;

public class dist_demo {

    public static void main(String []args) {

        List<Integer> aList_demo = new ArrayList<Integer> (5);

        aList_demo.add(50);
        aList_demo.add(100);
        aList_demo.add(150);
        aList_demo.add(200);
        aList_demo.add(250);

        System.out.println("List before using the remove method");
        System.out.println(aList_demo);
        System.out.println("\n");

        aList_demo.remove(2);       
        System.out.println("List after using the remove method");        
        System.out.println(aList_demo);
 }
}

Output:

List before using the remove method
[50, 100, 150, 200, 250]
List after using the remove method
[50, 100, 200, 250]
If there are duplicate elements in the list, only the first occurrence will be removed. If the element is found in the list, the method returns true otherwise false.

A demo of removing by object with duplicate entries

In this example, duplicate elements exist in the list. The remove method is used when an object is created with duplicate entries. See the output and code before and after using the remove method:

The code of remove method by object:

import java.util.*;

public class dist_demo {

    public static void main(String []args) {

        List<String> aList_demo = new ArrayList<String> (5);

        aList_demo.add("Apple");
        aList_demo.add("Mango");
        aList_demo.add("Orange");
        aList_demo.add("Mango");
        aList_demo.add("Banana");


        System.out.println("The fruit list before remove method");
        System.out.println(aList_demo);
        System.out.println("\n");
         aList_demo.remove("Mango");       
        System.out.println("The fruit list after remove method");       
        System.out.println(aList_demo);
 }

}

Output:

The fruit list before remove method
[Apple, Mango, Orange, Mango, Banana]
The fruit list after remove method
[Apple, Orange, Mango, Banana]

You can see in the output, the mango’s second occurrence still exists in the list after using the remove method.

Accessing list elements example

You may use the get method for accessing the specific elements of the list. In the first example of this tutorial, the get method is used in a for loop for displaying the complete list.

In the get() method, you may specify the index number that starts at 0. See this example where I created a list with four elements.

The value of elements with indexes 1 and 3 are displayed by using the get() method:

The code:

import java.util.*;

public class dist_demo {

    public static void main(String []args) {

        List<String> aList_demo = new ArrayList<String> (4);

        aList_demo.add("Apple");
        aList_demo.add("Mango");
        aList_demo.add("Orange");
        aList_demo.add("Banana");

        System.out.println("The list element at 1 index is: " + aList_demo.get(1));
        System.out.println("\n");
        System.out.println("The list element at 3 index is: " + aList_demo.get(3));

 }
}

Output:

The list element at 1 index is: Mango

The list element at 3 index is: Banana

Iterating through a list by using the iterator

As shown earlier, you may use a for loop for iterating through a list. Alternatively, you may use the list iterator for going through a list as shown in the example below:

The code for iterating through a list with a while loop:

import java.util.*;

public class dist_demo {

    public static void main(String []args) {
        List<String> aList_demo = new ArrayList<String>();
        //Iterator<String> iterator = aList_demo.iterator();
        aList_demo.add("Iterating");
        aList_demo.add("Through");
        aList_demo.add("a");
        aList_demo.add("List");

        Iterator<String> iterator = aList_demo.listIterator();
        while (iterator.hasNext()) {

            System.out.println("The list item: "+iterator.next());

        }
  }
}

Output:

The list item: Iterating
The list item: ThroughThe list item: a
The list item: List

You can see, the elements are displayed just like for loop.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!