Difference Between List and Set in Java
Last Updated :
29 Apr, 2022
The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements.
Declaration:
public abstract interface List extends Collection
The set interface in the java.util package and extends Collection interface is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the maths set. This interface contains the methods inherited from the Collection interface and adds a feature that restricts to insert the duplicate elements.
Declaration: The Set interface is declared as:
public interface Set extends Collection
Example:
Input : Add Elements = [1, 2, 3, 1]
Output: Set = [1, 2, 3]
List = [1, 2, 3, 1]
Input : Add Elements = [a, b, d, b]
Output: Set = [a, b, d]
List = [a, b, d, b]
Below is the illustration of Set and List :
Java
// Implementation of List and Set in Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// List declaration
List<Integer> l = new ArrayList<>();
l.add(5);
l.add(6);
l.add(3);
l.add(5);
l.add(4);
// Set declaration
Set<Integer> s = new HashSet<>();
s.add(5);
s.add(6);
s.add(3);
s.add(5);
s.add(4);
// printing list
System.out.println("List = " + l);
// printing Set
System.out.println("Set = " + s);
}
}
OutputList = [5, 6, 3, 5, 4]
Set = [3, 4, 5, 6]
Difference between List and Set:
List | Set |
---|
1. The List is an indexed sequence. | 1. The Set is an non-indexed sequence. |
2. List allows duplicate elements | 2. Set doesn't allow duplicate elements. |
3. Elements by their position can be accessed. | 3. Position access to elements is not allowed. |
4. Multiple null elements can be stored. | 4. Null element can store only once. |
5. List implementations are ArrayList, LinkedList, Vector, Stack | 5. Set implementations are HashSet, LinkedHashSet. |
Similar Reads
Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos
4 min read
Difference Between List and Set in C# The list is C# is the same as the list in JAVA. Basically, it is a type of object which can store variables. But in difference with objects, it stores the variables only in a specific order. Following is the syntax from which we can declare variables: Syntax: List<int> numbers = new List<in
2 min read
Difference between List and ArrayList in Java A Collection is a group of individual objects represented as a single unit. Java provides a Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit This framework consists of the List Interface as well as the ArrayList class. In this article
4 min read
Difference Between LinkedList and LinkedHashSet in Java In this article you will learn difference between LinkedList and LinkedHashSet in java. Prerequisite: LinkedList : LinkedHashSet LinkedList class implements the List and Deque interface and extends from AbstractSequentialList class. LinkedList class uses doubly linked list to store the elements. It
3 min read
Difference Between LinkedList and LinkedHashSet in Java In this article you will learn difference between LinkedList and LinkedHashSet in java. Prerequisite: LinkedList : LinkedHashSet LinkedList class implements the List and Deque interface and extends from AbstractSequentialList class. LinkedList class uses doubly linked list to store the elements. It
3 min read
Difference between HashMap and HashSet HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set. HashMap is an implementation of Map Interface,
4 min read