Difference between List, Set and Map in Java

Last Updated : 6 Jun, 2026

In Java Collections Framework, List, Set, and Map are the most commonly used collection types for storing and managing data. Each serves a different purpose based on how elements are stored, accessed, and organized. Understanding their differences helps in selecting the most suitable collection for a particular requirement.

  • List stores elements in an ordered sequence and allows duplicate values.
  • Set stores only unique elements and does not allow duplicates.
  • Map stores data in the form of key-value pairs, where each key must be unique.

Set Interface

The Set interface is a part of the java.util package and is used to store a collection of unique elements. It does not allow duplicate values and is commonly used when data uniqueness is required.

  • Stores only unique elements.
  • Duplicate values are automatically ignored.
  • Provides faster searching compared to a List in many implementations.
  • Common implementations are HashSet, LinkedHashSet, and TreeSet.

Syntax:

Set<DataType> setName = new HashSet<>();

Java
import java.util.HashSet;
import java.util.Set;

public class SetExample {
    public static void main(String[] args) {

        // Creating a Set
        Set<String> fruits = new HashSet<>();

        // Adding elements
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango");
        fruits.add("Orange");
        fruits.add("Apple");   // Duplicate element

        // Displaying Set elements
        System.out.println("Elements in Set: " + fruits);

        // Checking if an element exists
        System.out.println("Contains Mango? " + fruits.contains("Mango"));

        // Removing an element
        fruits.remove("Orange");

        // Displaying updated Set
        System.out.println("After removing Orange: " + fruits);

        // Size of Set
        System.out.println("Total Elements: " + fruits.size());
    }
}

Output
Elements in Set: [Apple, Mango, Orange, Banana]
Contains Mango? true
After removing Orange: [Apple, Mango, Banana]
Total Elements: 3

List Interface

The List interface is a part of the java.util package and is used to store an ordered collection of elements. It allows duplicate values and preserves the insertion order of elements.

  • Maintains the insertion order of elements.
  • Allows duplicate values.
  • Supports index-based access to elements.
  • Common implementations are ArrayList, LinkedList, and Vector.

Syntax:

List<DataType> listName = new ArrayList<>();

Java
import java.util.*;
public class GFG {

    public static void main(String args[])
    {

        // Creating a List
        List<String> al = new ArrayList<>();

        // Adding elements in the List
        al.add("mango");
        al.add("orange");
        al.add("Grapes");

        // Iterating the List
        // element using for-each loop
        for (String fruit : al)
            System.out.println(fruit);
    }
}

Output :

mango
orange
Grapes

Map Interface

The Map interface is a part of the java.util package and is used to store data in the form of key-value pairs. Each key must be unique, while values can be duplicated.

  • Stores data as key-value pairs.
  • Keys must be unique.
  • Values can be duplicated.
  • Common implementations are HashMap, LinkedHashMap, and TreeMap.

Syntax:

Map<KeyType, ValueType> mapName = new HashMap<>();

Java
import java.util.*;

class MapExample {

    public static void main(String args[])
    {

        // Creating object for Map.
        Map<Integer, String> map
            = new HashMap<Integer, String>();

        // Adding Elements using Map.
        map.put(100, "Amit");
        map.put(101, "Vijay");
        map.put(102, "Rahul");

        // Elements can traverse in any order
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}

Output :

100 Amit
101 Vijay
102 Rahul

List vs Set vs Map

FeatureListSetMap
Interface Packagejava.util.Listjava.util.Setjava.util.Map
Data StorageStores elementsStores unique elementsStores key-value pairs
Duplicate ValuesAllowedNot AllowedDuplicate keys not allowed, values allowed
Insertion OrderMaintains insertion orderDepends on implementationDepends on implementation
Index-Based AccessSupportedNot SupportedNot Supported
Key-Value PairNoNoYes
Null ValuesMultiple null values allowedUsually one null element allowedOne null key (HashMap) and multiple null values allowed
Retrieval MethodBy indexBy elementBy key
Primary UseOrdered collection of dataStoring unique dataAssociating keys with values
Common ImplementationsArrayList, LinkedList LinkedList, VectorHashSet, LinkedHashSet, TreeSetHashMap, LinkedHashMap, TreeMap
Comment