VOOZH about

URL: https://www.geeksforgeeks.org/java/difference-between-list-set-and-map-in-java/

⇱ Difference between List, Set and Map in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between List, Set and Map in Java

Last Updated : 6 Jun, 2026

In JavaCollections 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<>();


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<>();

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<>();

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