List vs Set vs Map in Java
Java Collections Framework provides various interfaces to store and manipulate groups of objects efficiently. The three primary interfaces in Java Collections are List, Set, and Map. Each of these serves a distinct purpose and is used in different scenarios depending on the requirements of an application.
In this blog post, we will explore the differences between List, Set, and Map in Java, their characteristics, implementations, and when to use them.
Understanding List, Set, and Map
1. List Interface
A List is an ordered collection (also known as a sequence) that allows duplicate elements. It maintains insertion order and allows positional access using an index.
Key Characteristics of List:
-
Allows duplicate elements.
-
Maintains insertion order.
-
Provides indexed access to elements.
-
Can contain
null
values.
Common Implementations of List:
-
ArrayList – A resizable array implementation.
-
LinkedList – A doubly-linked list implementation.
-
Vector – A synchronized, resizable array.
Example of List Usage:
import java.util.*;
public class ListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple"); // Allows duplicate
list.add("Cherry");
System.out.println(list); // Output: [Apple, Banana, Apple, Cherry]
}
}
2. Set Interface
A Set is an unordered collection that does not allow duplicate elements. It is useful when uniqueness is required, such as in membership checks.
Key Characteristics of Set:
-
Does not allow duplicate elements.
-
Unordered or sorted depending on implementation.
-
Allows at most one
null
value (except inTreeSet
, which does not allownull
).
Common Implementations of Set:
-
HashSet – Stores elements in a hash table (unordered, fast lookups).
-
LinkedHashSet – Maintains insertion order.
-
TreeSet – Stores elements in a sorted, tree-based structure.
Example of Set Usage:
import java.util.*;
public class SetExample {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate is ignored
set.add("Cherry");
System.out.println(set); // Output: [Banana, Cherry, Apple] (unordered)
}
}
3. Map Interface
A Map is a collection that stores elements in key-value pairs. It does not extend the Collection
interface but is part of the Java Collections Framework.
Key Characteristics of Map:
-
Stores key-value pairs.
-
Keys must be unique, but values can be duplicated.
-
Allows
null
keys (only one) and multiplenull
values.
Common Implementations of Map:
-
HashMap – Unordered, fast lookups.
-
LinkedHashMap – Maintains insertion order.
-
TreeMap – Stores keys in a sorted order.
Example of Map Usage:
import java.util.*;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 10);
map.put("Banana", 20);
map.put("Apple", 30); // Overwrites previous value
System.out.println(map); // Output: {Apple=30, Banana=20} (unordered)
}
}
Comparison: List vs Set vs Map
Feature | List | Set | Map |
---|---|---|---|
Allows duplicates | Yes | No | No (keys) |
Maintains order | Yes | LinkedHashSet does |
LinkedHashMap does |
Access by index | Yes | No | No (access by key) |
Key-Value Pair | No | No | Yes |
Allows null |
Yes | Yes (only one in HashSet ) |
Yes (one key, multiple values) |
Common Implementations | ArrayList , LinkedList |
HashSet , TreeSet |
HashMap , TreeMap |
When to Use List, Set, or Map?
Scenario | Recommended Collection |
---|---|
Need to store duplicate elements in order | List (e.g., ArrayList ) |
Need unique elements with fast lookup | Set (e.g., HashSet ) |
Need key-value mappings | Map (e.g., HashMap ) |
Need sorted unique elements | TreeSet or TreeMap |
Need to maintain insertion order | LinkedHashSet or LinkedHashMap |
Conclusion
Choosing between List, Set, and Map depends on the specific requirements of your application. If you need ordered elements with duplicates, use a List. If you require unique elements, use a Set. If you need to associate keys with values, use a Map. Understanding their differences and characteristics will help you write optimized and efficient Java applications.
Sign up here with your email
ConversionConversion EmoticonEmoticon