DEV Community

Gowtham Kalyan
Gowtham Kalyan

Posted on

Difference Between List, Set, and Map in Java

The Java Collections Framework provides powerful data structures to store and manage data efficiently. Among them, List, Set, and Map are the most commonly used interfaces in real-world Java applications.

Understanding their differences is very important for students, freshers, and developers because this is one of the most frequently asked Java interview questions.


✅ What is List?

A List is an ordered collection that allows duplicate elements. Elements are stored based on index positions.

Example:

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

List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("Java"); // duplicates allowed

System.out.println(list);
Enter fullscreen mode Exit fullscreen mode

Key Features:

  • Maintains insertion order
  • Allows duplicates
  • Supports index-based access
  • Example classes: ArrayList, LinkedList, Vector

✅ What is Set?

A Set is a collection that does not allow duplicate elements.

Example:

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

Set<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("Java"); // duplicate ignored

System.out.println(set);
Enter fullscreen mode Exit fullscreen mode

Key Features:

  • No duplicate values
  • Unordered (HashSet)
  • Faster search operations
  • Example classes: HashSet, LinkedHashSet, TreeSet

✅ What is Map?

A Map stores data in key-value pairs. Keys must be unique, but values can be duplicated.

Example:

import java.util.HashMap;
import java.util.Map;

Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
map.put(1, "Spring"); // replaces previous value

System.out.println(map);
Enter fullscreen mode Exit fullscreen mode

Key Features:

  • Stores key-value pairs
  • Keys are unique
  • Not part of Collection interface
  • Example classes: HashMap, LinkedHashMap, TreeMap, Hashtable

📊 Difference Between List, Set, and Map

Feature List Set Map
Order Maintained Yes Depends on implementation Depends on implementation
Duplicate Elements Allowed Not Allowed Keys not allowed, Values allowed
Data Storage Single values Unique values Key-Value pairs
Index Access Yes No No
Null Values Allowed Usually one null One null key (HashMap)
Part of Collection Interface Yes Yes No

✅ When Should You Use Each?

  • List → When order and duplicates matter.
  • Set → When uniqueness is required.
  • Map → When data must be stored as key-value relationships.

🎯 Interview Tip

Interviewers often ask:

👉 Why Map is not part of Collection interface?

Because Map stores elements as key-value pairs, while Collection handles only single objects.


🚀 Top Java Real Time Projects Online Training in Hyderabad

Want to master Java Collections, Spring Boot, REST APIs, and real-time development concepts through practical learning?

Join industry-oriented training designed for students and job seekers.

👉 Enroll Now:

Top Java Real Time Projects Online Training in Hyderabad

✔ Core Java & Collections deep understanding
✔ Real-time project implementation
✔ Spring Boot & Microservices training
✔ Interview preparation support
✔ Practical learning with real-time experts at ashok it

Build strong Java development skills and become industry-ready with hands-on project experience.

Top comments (0)