Java List vs Set

Both the List interface and the Set interface inherits the Collection interface. However, there exists some differences between them.

List

Set

The List is an ordered sequence. The Set is an unordered sequence.
List allows duplicate elements Set doesn’t allow duplicate elements.
Elements by their position can be accessed. Position access to elements is not allowed.
Multiple null elements can be stored. The null element can store only once.
List implementations are ArrayList, LinkedList, Vector, Stack Set implementations are HashSet, LinkedHashSet.

Classes Association with a Java List Interface

Now let us discuss the classes that implement the List Interface for which first do refer to the pictorial representation below to have a better understanding of the List interface. It is as follows: 

AbstractList, CopyOnWriteArrayList, and the AbstractSequentialList are the classes that implement the List interface. A separate functionality is implemented in each of the mentioned classes. They are as follows:

  1. AbstractList: This class is used to implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement only the get() and the size() methods.
  2. CopyOnWriteArrayList: This class implements the list interface. It is an enhanced version of ArrayList in which all the modifications(add, set, remove, etc.) are implemented by making a fresh copy of the list.
  3. AbstractSequentialList: This class implements the Collection interface and the AbstractCollection class. This class is used to implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement only the get() and the size() methods.

We will proceed in this manner. 

  • ArrayList
  • Vector
  • Stack
  • LinkedList

Let us discuss them sequentially and implement the same to figure out the working of the classes with the List interface. 

1. ArrayList

An ArrayList class which is implemented in the collection framework provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. Let’s see how to create a list object using this class. 

Example:

Java




// Java program to demonstrate the
// creation of list object using the
// ArrayList class
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Size of ArrayList
        int n = 5;
  
        // Declaring the List with initial size n
        List<Integer> arrli = new ArrayList<Integer>(n);
  
        // Appending the new elements
        // at the end of the list
        for (int i = 1; i <= n; i++)
            arrli.add(i);
  
        // Printing elements
        System.out.println(arrli);
  
        // Remove element at index 3
        arrli.remove(3);
  
        // Displaying the list after deletion
        System.out.println(arrli);
  
        // Printing elements one by one
        for (int i = 0; i < arrli.size(); i++)
            System.out.print(arrli.get(i) + " ");
    }
}


Output

[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5 

2. Vector

Vector is a class that is implemented in the collection framework implements a growable array of objects. Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. Vectors basically fall in legacy classes but now it is fully compatible with collections. Let’s see how to create a list object using this class. 

Example:

Java




// Java program to demonstrate the
// creation of list object using the
// Vector class
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Size of the vector
        int n = 5;
  
        // Declaring the List with initial size n
        List<Integer> v = new Vector<Integer>(n);
  
        // Appending the new elements
        // at the end of the list
        for (int i = 1; i <= n; i++)
            v.add(i);
  
        // Printing elements
        System.out.println(v);
  
        // Remove element at index 3
        v.remove(3);
  
        // Displaying the list after deletion
        System.out.println(v);
  
        // Printing elements one by one
        for (int i = 0; i < v.size(); i++)
            System.out.print(v.get(i) + " ");
    }
}


Output

[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5 

3. Stack

Stack is a class that is implemented in the collection framework and extends the vector class models and implements the Stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions of empty, search and peek. Let’s see how to create a list object using this class.

Example:

Java




// Java program to demonstrate the
// creation of list object using the
// Stack class
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Size of the stack
        int n = 5;
  
        // Declaring the List
        List<Integer> s = new Stack<Integer>();
  
        // Appending the new elements
        // at the end of the list
        for (int i = 1; i <= n; i++)
            s.add(i);
  
        // Printing elements
        System.out.println(s);
  
        // Remove element at index 3
        s.remove(3);
  
        // Displaying the list after deletion
        System.out.println(s);
  
        // Printing elements one by one
        for (int i = 0; i < s.size(); i++)
            System.out.print(s.get(i) + " ");
    }
}


Output

[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5 

4. LinkedList

LinkedList is a class that is implemented in the collection framework which inherently implements the linked list data structure. It is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. Let’s see how to create a list object using this class.

Example:

Java




// Java program to demonstrate the
// creation of list object using the
// LinkedList class
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Size of the LinkedList
        int n = 5;
  
        // Declaring the List with initial size n
        List<Integer> ll = new LinkedList<Integer>();
  
        // Appending the new elements
        // at the end of the list
        for (int i = 1; i <= n; i++)
            ll.add(i);
  
        // Printing elements
        System.out.println(ll);
  
        // Remove element at index 3
        ll.remove(3);
  
        // Displaying the list after deletion
        System.out.println(ll);
  
        // Printing elements one by one
        for (int i = 0; i < ll.size(); i++)
            System.out.print(ll.get(i) + " ");
    }
}


Output

[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5 



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