Collections in Java – List, Set, and Map Explained
Java Collections Framework is a powerful architecture that provides classes and interfaces to store and manipulate groups of objects. Collections are widely used for data storage, retrieval, sorting, and manipulation. The List, Set, and Map are among the most commonly used collection types in Java, each serving unique purposes and offering specific advantages.
In this comprehensive guide, we will explore:
- The differences between List, Set, and Map in Java.
- How to use ArrayList, HashSet, and HashMap effectively.
- Practical examples, best practices, and real-world use cases.
📖 Learn more: Oracle Documentation – Collections in Java
What is the Java Collections Framework?
The Java Collections Framework (JCF) is a set of classes and interfaces that implement reusable data structures. It includes Collection interfaces, such as List, Set, and Queue, and Map interfaces, such as HashMap and TreeMap.
Key Components of the Collections Framework:
- Collection Interface: The root interface for List, Set, and Queue.
- Map Interface: Not part of Collection, but provides a key-value mapping.
- Utility Classes: Such as Collections and Arrays for sorting, searching, and thread-safe collections.
📖 Read more: GeeksforGeeks – Java Collections Framework
1. The List Interface
A List is an ordered collection that allows duplicate elements. It provides positional access and maintains the insertion order.
Common Implementations of List:
- ArrayList: Provides dynamic array capabilities.
- LinkedList: Ideal for frequent insertions and deletions.
- Vector: Thread-safe version of ArrayList, but generally slower.
Example: Using ArrayList in Java
import java.util.ArrayList;
public class ListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // Allows duplicates
fruits.add("Orange");
System.out.println(fruits);
}
}
When to Use a List?
- When order matters.
- When duplicates are allowed.
- When indexed access is needed.
📖 Learn more: Baeldung – Java List Interface
2. The Set Interface
A Set is a collection that does not allow duplicate elements. Sets are ideal for storing unique values and do not maintain insertion order (in HashSet).
Common Implementations of Set:
- HashSet: Unordered, allows null, and is fast for add, remove, and search operations.
- LinkedHashSet: Maintains insertion order.
- TreeSet: Sorted set based on natural order or comparator.
Example: Using HashSet in Java
import java.util.HashSet;
public class SetExample {
public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // Duplicate, will not be added
fruits.add("Orange");
System.out.println(fruits);
}
}
When to Use a Set?
- When uniqueness of elements is required.
- When no duplicates are allowed.
- When fast operations are needed.
📖 Learn more: W3Schools – Java Set Interface
3. The Map Interface
A Map is a collection that maps keys to values, where each key is unique. Unlike List and Set, Map is not part of the Collection interface but is still a core part of the Collections Framework.
Common Implementations of Map:
- HashMap: Allows null keys and values, provides constant-time performance for basic operations.
- LinkedHashMap: Maintains insertion order.
- TreeMap: Sorted map based on natural order or comparator.
Example: Using HashMap in Java
import java.util.HashMap;
public class MapExample {
public static void main(String[] args) {
HashMap<String, Integer> fruitQuantities = new HashMap<>();
fruitQuantities.put("Apple", 10);
fruitQuantities.put("Banana", 20);
fruitQuantities.put("Orange", 15);
System.out.println(fruitQuantities);
}
}
When to Use a Map?
- When key-value pairs are required.
- When unique keys are needed.
- When fast lookups by key are essential.
📖 Learn more: JavaTPoint – Java Map Interface
Key Differences: List vs Set vs Map
Feature | List | Set | Map |
---|---|---|---|
Duplicates | Allowed | Not allowed | Keys not allowed, values are |
Order | Maintains insertion order | Unordered (HashSet) | Not maintained (HashMap) |
Null Values | Allows multiple nulls | Allows one null | One null key, multiple null values |
Access | Index-based | No index | Key-based |
📖 Read more: Baeldung – List vs Set vs Map in Java
Frequently Asked Questions (FAQ)
1. What is the difference between List, Set, and Map in Java?
- List: Allows duplicates, maintains insertion order, and provides indexed access.
- Set: Does not allow duplicates, may or may not maintain order, no indexed access.
- Map: Key-value pairs, unique keys, does not implement Collection interface.
2. When should I use ArrayList over HashSet in Java?
Use ArrayList when order matters and duplicates are allowed. Use HashSet when you need unique elements and order is not important.
3. Can I convert a List to a Set in Java?
Yes, you can convert List to Set using:
Set<String> set = new HashSet<>(list);
This will remove duplicates from the List.
4. What is the best choice for key-value data in Java?
Use Map (e.g., HashMap) when you need to store data as key-value pairs.
5. Can a Set contain duplicate null values?
No, a Set can contain only one null value, while List allows multiple null values.
📖 Learn more: Programiz – Java Collections Framework
Conclusion
Understanding the differences between List, Set, and Map in Java is essential for choosing the right data structure for your application. By mastering ArrayList, HashSet, and HashMap, you can optimize performance, maintain data integrity, and write cleaner code.
🚀 Now that you know about collections, let’s see how to order and manipulate these structures!