The Java Collections Framework provides ready-made data structures for storing, searching, sorting, and processing groups of objects. It includes List, Set, Queue, Map, and many implementations.
Choosing the right collection affects readability and performance. ArrayList, HashSet, HashMap, LinkedList, TreeSet, and PriorityQueue each solve different problems.
Collections need notes about choosing the right structure. ArrayList is usually the default list, HashMap is for key-value lookup, Set is for uniqueness, and Queue is for ordered processing.
When revising collections, always connect each interface with a real job: List keeps ordered items, Set removes duplicates, Map finds values by key, and Queue processes work in order. This practical mapping makes collection selection easier than memorizing class names alone.
Also note the cost of common operations. Fast lookup, fast insertion, predictable order, and memory usage are different goals, so choosing a collection should be based on the operation your program performs most often.
Finally, revise iteration safety: changing a collection while reading it can cause skipped data or runtime errors, so filtering, copying, or iterator-based removal should be part of every collections practice session.
Each main collection family has a different contract. Learn the contract first, then choose an implementation.
| Interface | Allows Duplicates? | Key Feature | Common Implementation |
|---|---|---|---|
| List | Yes | Index-based ordered data | ArrayList |
| Set | No | Unique values | HashSet |
| Map | Keys unique | Key-value lookup | HashMap |
| Queue | Usually yes | Processing order | ArrayDeque |
ArrayList is the default choice for most list work. LinkedList is useful mainly when frequent insertions/removals happen at both ends and random access is not needed.
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List<String> skills = new ArrayList<>();
skills.add("Java");
skills.add("SQL");
skills.add("Spring");
System.out.println(skills.get(0));
System.out.println(skills.contains("SQL"));
}
}
HashSet is used for uniqueness. HashMap is used for fast lookup by key. Both depend on hashCode and equals for custom objects.
import java.util.HashMap;
import java.util.Map;
public class FrequencyCount {
public static void main(String[] args) {
String text = "banana";
Map<Character, Integer> freq = new HashMap<>();
for (char ch : text.toCharArray()) {
freq.put(ch, freq.getOrDefault(ch, 0) + 1);
}
System.out.println(freq);
}
}
Use Collections.sort for lists of comparable values, or pass a Comparator for custom order.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortDemo {
public static void main(String[] args) {
List<String> names = new ArrayList<>(List.of("Ravi", "Asha", "Meera"));
names.sort(Comparator.comparingInt(String::length).thenComparing(String::compareTo));
System.out.println(names);
}
}
Use List when order matters and duplicates are allowed. Use Set when uniqueness matters. Use Map when lookup by key matters. Use Queue or Deque when processing order matters more than random access.
ArrayList uses a dynamic array - fast random access O(1) but slow insert/delete in middle O(n). LinkedList uses doubly-linked nodes - fast insert/delete at ends O(1) but slow random access O(n). Use ArrayList for most cases.
HashMap is not synchronized (not thread-safe) and allows one null key. Hashtable is synchronized (thread-safe) but slower, and does not allow null keys. For thread safety, use ConcurrentHashMap instead of Hashtable.
HashMap uses an array of buckets. The key's hashCode() determines the bucket index. If multiple keys hash to the same bucket (collision), they are stored in a linked list (or red-black tree in Java 8+ when size > 8).
Explore 500+ free tutorials across 20+ languages and frameworks.