Java List Interface

In java List interface is an interface that extends the collection interface. It is one of the most important interfaces of the collection framework. Basically, the java List interface is implemented by different classes, that can store the collection of elements and each element stored on the basis of Index. It can contain duplicate elements as well. The List interface inherits the methods from Collection. The List interface has its implementation in various classes like ArrayList, LinkedList, Vector and Stack. We will discuss each class in a separate post.

Java List Interface

List interface properties

Java List interface has some properties that are used to handle the collections of objects. Whoever class implements the List interface, that gets all the properties automatically.

1. Java List interface provides the functionality to add duplicate elements in the list. In collection framework ArrayList, LinkedList, Vector, Stack are implementing it, It means they all can store the duplicate elements.

2. Java List interface allows us to have ‘null’ elements in the Java list.

3. Since Java 8, List interface has some default methods, those are automatically inherited in derived classes

4. We can make a list as Generic in java to avoid ClassCastException at runtime.

Important Methods

Java list interface has a lots of method, some methods are inherited from collection interface and some are its own. We will discuss the most common and useful method of list.

1. int size(): It returns the size of the list. The size of the list means how many elements presented in list.
2. boolean isEmpty(): To find whether the list is empty or not. If the list is empty it returns true otherwise false.
3. boolean contains(Object o): To find whether the given Object o exists or not. It returns true if the list contains the given element.
4. Iterator iterator(): To get an iterator so that you can iterate over the elements in the list in the proper sequence.
5. Object[] toArray(): It returns an array that contains all the elements of the list in proper sequence
6. boolean add(E e): The add() method is used to append given element e to the end of this list.
7. boolean remove(Object o): The remove() method is used to remove the first occurrence of the given element from the list.
8. boolean retainAll(Collection c): This method is used to retains the elements in the list, those are also contained in the given collection c.
9. void clear(): To remove all the elements from the list.
10. E get(int index): To get the element from the specified position in the list.
11. E set(int index, E element): This method is used to add the element at a specific position. If any element is already there, then it Replaces the element at the specified position.
12. ListIterator listIterator(): It returns a list iterator over the elements in the list.
13. List subList(int fromIndex, int toIndex): By use of this method we can get a sublist from the list. It means to get a portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive.

Implementation of the List Interface

The Java list interface is implemented by ArrayList, LinkedList, Vector, and Stack. Here we will discuss the most popular classes that are ArrayList and LinkedList.

Let’s see the how ArrayList implements List interface and use it.

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
package package1;

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

class ArrayListExample {

    public static void main(String[] args) {
        // Creating list using the ArrayList class
        List<String> names = new ArrayList<String>();

        // Add elements to the list
        names.add("Hi");
        names.add("JavaGoal");
        names.add("Website");
        System.out.println("List: " + names);

        // Access element from the list
        String name = names.get(1);
        System.out.println("Accessed Element: " + name);

        // Remove element from the list
        names.remove("Hi");
        System.out.println("After Removed name: " + names);
    }
}

Output: List: [Hi, JavaGoal, Website]
Accessed Element: JavaGoal
After Removed name: [JavaGoal, Website]

Let’s see the how LinkedList implements List interface and use it.

public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
package package1;

import java.util.LinkedList;
import java.util.List;

class LinkedListExample {

    public static void main(String[] args) {
        // Creating list using the LinkedList class
        List<String> names = new LinkedList<String>();

        // Add elements to the list
        names.add("Hi");
        names.add("JavaGoal");
        names.add("Website");
        System.out.println("List: " + names);

        // Access element from the list
        String name = names.get(1);
        System.out.println("Accessed Element: " + name);

        // Remove element from the list
        names.remove("Hi");
        System.out.println("After Removed name: " + names);
    }
}

Output: List: [Hi, JavaGoal, Website]
Accessed Element: JavaGoal
After Removed name: [JavaGoal, Website]

Leave a Comment