Java tutorials > Core Java Fundamentals > Data Structures and Collections > What is the difference between `List`, `Set`, and `Map`?
What is the difference between `List`, `Set`, and `Map`?
List
, Set
, and Map
. Understanding these differences is crucial for choosing the right data structure for a specific task, optimizing performance, and writing efficient Java code. We will cover their core characteristics, use cases, and provide code examples to illustrate their behavior.
Core Differences at a Glance
List in Detail
ArrayList
is a common implementation. Elements are stored in the order they are added. Duplicates are permitted. Elements can be accessed by their index using the get()
method. Other common List implementations include LinkedList
and Vector
.
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple"); // Duplicate allowed
list.add("Orange");
System.out.println(list); // Output: [Apple, Banana, Apple, Orange]
System.out.println(list.get(1)); // Output: Banana
}
}
Set in Detail
HashSet
is a common implementation. When you try to add a duplicate element, it's simply ignored. The order of elements in a Set is not guaranteed and may vary. Other common Set implementations include TreeSet
(which maintains elements in sorted order) and LinkedHashSet
(which maintains insertion order).
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate ignored
set.add("Orange");
System.out.println(set); // Output: [Orange, Banana, Apple] (Order not guaranteed)
}
}
Map in Detail
HashMap
is a commonly used implementation. If you try to insert a duplicate key with a different value, the existing value is overwritten. The get()
method retrieves the value associated with a specific key. Other common Map implementations include TreeMap
(which maintains keys in sorted order) and LinkedHashMap
(which maintains insertion order).
import java.util.HashMap;
import java.util.Map;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Orange", 3);
map.put("Apple", 4); // Overwrites the previous value for "Apple"
System.out.println(map); // Output: {Orange=3, Banana=2, Apple=4}
System.out.println(map.get("Apple")); // Output: 4
}
}
Real-Life Use Case Section
Best Practices
Interview Tip
When to use them
Memory Footprint
ArrayList
typically allocates extra memory to avoid frequent array resizing which impacts the memory footprint.
Alternatives
List
, Set
, and Map
are fundamental, other specialized collections exist, such as Queue
, Deque
, and custom data structures tailored to specific needs. Arrays can also be used when the size is known in advance and mutability is not required. Libraries like Guava provide more advanced collection implementations.
Pros and Cons
Set:
Map:
Concepts behind the snippet
List
) allows you to specify the type of objects that the collection will hold, providing compile-time type safety.
FAQ
-
When should I use a LinkedList instead of an ArrayList?
Use a LinkedList when you need frequent insertions or deletions in the middle of the list, as these operations are more efficient in a LinkedList (O(1)) than in an ArrayList (O(n)). ArrayLists are better for random access (getting an element by index). -
How do I iterate over a Set?
You can iterate over a Set using an Iterator or a for-each loop:Set
set = new HashSet<>(); // Add elements to the set for (String element : set) { System.out.println(element); } -
How do I ensure that my custom objects can be used as keys in a HashMap?
Your custom class must override theequals()
andhashCode()
methods correctly. ThehashCode()
method must return the same value for two objects that are equal according to theequals()
method. If you don't do this properly, you may not be able to retrieve objects from the map correctly.