List interface in Java | PLM Developer

In Java, the List is an interface that extends the Collection interface. List interface allows to store and access elements sequentially. It is an ordered collection of objects with duplicate values that can be stored and preserve the insertion order.

The list is an interface so we cannot provide a direct implementation of it. We can use the following classes to implement the functionalities of List.


4. Stack

The list is present in java.util package we must import java.util.List before using it.

List<Integer> list1 = ArrayList<>();
List<Integer> list2 = LinkedList<>();
List<Integer> list3 = Vector<>();
List<Integer> list4 = Stack<>();

ArrayList implementation of List:

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

public class ArrayListImpl {

    public static void main(String[] args) {
        //create List with ArrayList class
        List<Integer> numbersList = new ArrayList<>();
        //Ading elements to list
        numbersList.add(1);
        numbersList.add(2);
        numbersList.add(3);
        numbersList.add(4);

        System.out.println(“List values: “+numbersList);

        //Access elements from list without removing
        int getNumber = numbersList.get(2);
        System.out.println(“Element at index 2 is: “+getNumber);

        //Removing element from the list
        int removedNumber = numbersList.remove(2);
        System.out.println(“Removed Number at index 2 is: “+removedNumber);

        //Printing the list after removing the element
        System.out.println(“List values: “+numbersList);
    }
}
output:
List values: [1, 2, 3, 4]
Element at index 2 is: 3
Removed Number at index 2 is: 3
List values: [1, 2, 4]

LinkedList implementation of List:

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

public class LinkedListImpl {

    public static void main(String[] args) {
        //create List with LinkedList class
        List<Integer> numbersList = new LinkedList<>();
        //Ading elements to list
        numbersList.add(1);
        numbersList.add(2);
        numbersList.add(3);
        numbersList.add(4);

        System.out.println(“List values: “+numbersList);

        //Access elements from list without removing
        int getNumber = numbersList.get(2);
        System.out.println(“Element at index 2 is: “+getNumber);

        //Removing element from the list
        int removedNumber = numbersList.remove(2);
        System.out.println(“Removed Number at index 2 is: “+removedNumber);

        //Printing the list after removing the element
        System.out.println(“List values: “+numbersList);
    }
}
output:
List values: [1, 2, 3, 4]
Element at index 2 is: 3
Removed Number at index 2 is: 3
List values: [1, 2, 4]

Difference between List & Set:

  • The List is an ordered collection of objects it maintains insertion order.  
  • Set is an unordered collection of object it doesn’t maintain insertion order except LinkedHashSet.
  • List allows duplicate values.
  • The Set doesn’t allow duplicate values.
  • The List permits multiple null values.
  • The Set allows only one null value.
  • List implementations ArrayList, LinkedList
  • Set implementations HashSet, TreeSet.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top