top of page

List interface in Java

Updated: Dec 12, 2021

what is a List?

The list is the most versatile data type available in functional programming languages used to store a collection of similar data items. The concept is similar to arrays in object-oriented programming. List items can be written in a square bracket separated by commas.


what is the List interface in java?

The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.


Let us elaborate on creating objects or instances in a List class. Since List is an interface, objects cannot be created of the type list. We always need a class that implements this List in order to create an object.


It is possible to restrict the type of object that can be stored in the List. Just like several other user-defined ‘interfaces’ implemented by user-defined ‘classes‘, List is an ‘interface’, implemented by the ArrayList class, pre-defined in the java.util package.


List<Obj> list = new ArrayList<Obj> (); 

Some basic operations in a List interface:

Since List is an interface, it can be used only with a class that implements this interface.


Add Operation: Adding elements to the List class using add() method In order to add an element to the list, we can use the add() method. This method is overloaded to perform multiple operations based on different parameters.

  • add(Object): This method is used to add an element at the end of the List.

  • add(int index, Object): This method is used to add an element at a specific index in the List

Example:

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("Coders");
        list.add(1, "Arts");
        System.out.print(list);

    }
}

Output:


[Coders, Arts]

Update Operation: Updating elements after adding the elements, if we wish to change the element, it can be done using the set() method. Since List is indexed, the element which we wish to change is referenced by the index of the element. Therefore, this method takes an index and the updated element which needs to be inserted at that index.


Example:


import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("Coders");
        list.add(1, "Coders");
        System.out.print(list);
        list.set(1,"Arts");
        System.out.print(" Updated To -->");
        System.out.println(list);

    }
}

Output:


[Coders, Coders] Updated To -->[Coders, Arts]

Remove Operation: Removing Elements

In order to remove an element from a List, we can use the remove() method. This method is overloaded to perform multiple operations based on different parameters.

  • remove(Object): This method is used to simply remove an object from the List. If there are multiple such objects, then the first occurrence of the object is removed.

  • remove(int index): Since a List is indexed, this method takes an integer value which simply removes the element present at that specific index in the List. After removing the element, all the elements are moved to the left to fill the space and the indices of the objects are updated.

Example:

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("Coders");
        list.add(1, "Coders");
        list.remove(1);
        list.add("Arts");
        System.out.println(list);

    }
}

Output:


[Coders, Arts]

Methods of the List Interface:


The list interface contains the following methods

  • add(int index, element): This method is used to add an element at a particular index in the list. When a single parameter is passed, it simply adds the element at the end of the list.

  • addAll(int index, Collection collection): This method is used to add all the elements in the given collection to the list. When a single parameter is passed, it adds all the elements of the given collection at the end of the list.

  • size(): This method is used to return the size of the list.

  • clear(): This method is used to remove all the elements in the list. However, the reference of the list created is still stored.

  • remove(int index): This method removes an element from the specified index. It shifts subsequent elements(if any) to left and decreases their indexes by 1.

  • remove(element): This method is used to remove the first occurrence of the given element in the list.

  • get(int index): This method returns elements at the specified index.

  • set(int index, element): This method replaces elements at a given index with the new element. This function returns the element which was just replaced by a new element.

  • indexOf(element): This method returns the first occurrence of the given element or -1 if the element is not present in the list.

  • lastIndexOf(element): This method returns the last occurrence of the given element or -1 if the element is not present in the list.

  • equals(element): This method is used to compare the equality of the given element with the elements of the list.

  • hashCode(): This method is used to return the hashcode value of the given list.

  • isEmpty(): This method is used to check if the list is empty or not. It returns true if the list is empty, else false.

  • contains(element): This method is used to check if the list contains the given element or not. It returns true if the list contains the element.

  • containsAll(Collection collection): This method is used to check if the list contains all the collection of elements.

  • sort(Comparator comparator): This method is used to sort the elements of the list on the basis of the given comparator.

How does CodersArts help you in Java coding?

CodersArts provide :

  • Java assignment Help

  • Help in Java development Projects

  • Mentorship from Experts Live 1:1 session

  • Course and Project completions

  • CourseWork help





bottom of page