Methods of the List Interface

Since the main concept behind the different types of lists is the same, the list interface contains the following methods:

Method

Description

add(int index, element) This method is used with Java List Interface 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 with List Interface in Java 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 with Java List Interface 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 with Java List Interface 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 with Java List Interface to compare the equality of the given element with the elements of the list.
hashCode() This method is used with List Interface in Java to return the hashcode value of the given list.
isEmpty() This method is used with Java List Interface 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 with List Interface in Java 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 with Java List Interface to check if the list contains all the collection of elements.
sort(Comparator comp) This method is used with List Interface in Java to sort the elements of the list on the basis of the given comparator.

List Interface in Java with Examples

The List interface in Java 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. 

Table of Content

  • List Interface in Java
  • Declaration of Java List Interface
  • Example of Java List
  • Operations in a Java List Interface
  • Complexity of List Interface in Java
  • Iterating over List Interface in Java
  • Methods of the List Interface
  • Java List vs Set

Similar Reads

List Interface in Java

The List interface is found in java.util package and inherits the Collection interface. It is a factory of the ListIterator interface. Through the ListIterator, we can iterate the list in forward and backward directions. The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector. ArrayList and LinkedList are widely used in Java programming. The Vector class is deprecated since Java 5....

Declaration of Java List Interface

public interface List extends Collection ;...

Example of Java List

Java // Java program to Demonstrate List Interface    // Importing all utility classes import java.util.*;    // Main class // ListDemo class class GFG {        // Main driver method     public static void main(String[] args)     {            // Creating an object of List interface         // implemented by the ArrayList class         List l1 = new ArrayList();            // Adding elements to object of List interface         // Custom inputs            l1.add(0, 1);         l1.add(1, 2);            // Print the elements inside the object         System.out.println(l1);            // Now creating another object of the List         // interface implemented ArrayList class         // Declaring object of integer type         List l2 = new ArrayList();            // Again adding elements to object of List interface         // Custom inputs         l2.add(1);         l2.add(2);         l2.add(3);            // Will add list l2 from 1 index         l1.addAll(1, l2);            System.out.println(l1);            // Removes element from index 1         l1.remove(1);            // Printing the updated List 1         System.out.println(l1);            // Prints element at index 3 in list 1         // using get() method         System.out.println(l1.get(3));            // Replace 0th element with 5         // in List 1         l1.set(0, 5);            // Again printing the updated List 1         System.out.println(l1);     } }...

Operations in a Java List Interface

...

Complexity of List Interface in Java

Since List is an interface, it can be used only with a class that implements this interface. Now, let’s see how to perform a few frequently used operations on the List....

Iterating over List Interface in Java

...

Methods of the List Interface

...

Java List vs Set

...

Contact Us