☕ Java
List, Set and Map in Java
Three interfaces, dozens of real-world use cases. List keeps order and allows duplicates. Set enforces uniqueness. Map connects keys to values. Picking the right one isn't just best practice — it directly affects performance and correctness.
The Three Core Collection Interfaces
The Java Collections Framework is built around three fundamental interfaces:
• List — An ordered sequence. Duplicates allowed. Access by index. Think: shopping cart, playlist, queue of tasks.
• Set — An unordered pool. No duplicates — ever. Think: unique visitor IDs, tags on a post, permissions granted to a user.
• Map — A lookup table of key-value pairs. Keys are unique. Think: dictionary, cache, config settings, user ID → profile.
List Implementations
• ArrayList — backed by a dynamic array. Fast random access (get by index). Slow inserts/deletes in the middle. 95% of the time, use this.
• LinkedList — backed by linked nodes. Fast inserts/deletes anywhere. Slow random access. Use when you frequently add/remove from the front or middle.
Set — Enforcing Uniqueness
The moment you need to eliminate duplicates or answer "does this already exist?", reach for a Set.
Java
import java.util.HashSet;
import java.util.TreeSet;
import java.util.LinkedHashSet;
// HashSet — fastest, no guaranteed order
HashSet<String> tags = new HashSet<>();
tags.add("java");
tags.add("programming");
tags.add("java"); // duplicate — silently ignored
System.out.println(tags.size()); // 2, not 3
// TreeSet — sorted order automatically
TreeSet<Integer> sortedIds = new TreeSet<>();
sortedIds.add(300); sortedIds.add(100); sortedIds.add(200);
System.out.println(sortedIds); // [100, 200, 300] — always sorted
// LinkedHashSet — maintains insertion order
LinkedHashSet<String> history = new LinkedHashSet<>();
history.add("home"); history.add("products"); history.add("checkout");
System.out.println(history); // [home, products, checkout]Choosing the Right Collection
A quick decision guide for the most common scenarios:
• Need an ordered list with duplicates and fast access by index? → ArrayList
• Need fast insertions/deletions at both ends? → LinkedList (as a Deque)
• Need to store unique items without caring about order? → HashSet
• Need unique items in sorted order? → TreeSet
• Need unique items in insertion order? → LinkedHashSet
• Need fast key-based lookups without caring about order? → HashMap
• Need key-value pairs in sorted key order? → TreeMap
• Need key-value pairs in insertion order? → LinkedHashMap
Related Topics in Collections Framework
ArrayList
ArrayList is a resizable array — it grows as you add elements and shrinks as you remove them. It's the most used collection in Java, and for good reason: it combines the speed of arrays with the flexibility of dynamic sizing.
HashMap
HashMap stores data as key-value pairs — like a dictionary or a phone book. You look up values by key in constant time O(1). It's one of the most powerful and commonly used data structures in real applications.