STUDYECRAT Java : List vs Set vs Map
60s
Java List vs Set vs Map
Prove your skills in this interactive quiz
Live Code
Run snippets directly
Timed
60s per question
Scored
Earn 3D badges
★ Java Collections: List vs Set vs Map Key Differences
1. Duplicate Element Handling
List<String> list = new ArrayList<>();
list.add("A"); list.add("A"); // Allowed
Set<String> set = new HashSet<>();
set.add("A"); set.add("A"); // Rejected
Map<String, Integer> map = new HashMap<>();
map.put("A", 1); map.put("A", 2); // Overwrites value
// List allows duplicates, Set rejects them, Map overwrites values
list.add("A"); list.add("A"); // Allowed
Set<String> set = new HashSet<>();
set.add("A"); set.add("A"); // Rejected
Map<String, Integer> map = new HashMap<>();
map.put("A", 1); map.put("A", 2); // Overwrites value
// List allows duplicates, Set rejects them, Map overwrites values
- Tip: Use Set when unique elements are required without manual checks
- Real Use: Shopping cart (List) vs Unique coupon codes (Set) vs Product inventory (Map)
2. Null Element Support
List<String> list = new ArrayList<>();
list.add(null); // Allowed
Set<String> set = new TreeSet<>();
set.add(null); // Throws NullPointerException
Map<String, Integer> map = new HashMap<>();
map.put(null, 1); // Allowed (1 null key)
// TreeSet rejects nulls, HashMap allows 1 null key
list.add(null); // Allowed
Set<String> set = new TreeSet<>();
set.add(null); // Throws NullPointerException
Map<String, Integer> map = new HashMap<>();
map.put(null, 1); // Allowed (1 null key)
// TreeSet rejects nulls, HashMap allows 1 null key
- Tip: Use LinkedList if frequent null additions are needed
- Real Use: Configuration values where absence (null) is valid
3. Order Guarantees
List<Integer> arrayList = new ArrayList<>(List.of(3,1,2));
System.out.println(arrayList); // [3,1,2] (Insertion order)
Set<Integer> treeSet = new TreeSet<>(List.of(3,1,2));
System.out.println(treeSet); // [1,2,3] (Natural order)
Map<Integer, String> linkedMap = new LinkedHashMap<>();
linkedMap.put(3,"C"); linkedMap.put(1,"A");
System.out.println(linkedMap); // {3=C, 1=A} (Insertion order)
// Different collection types maintain different orders
System.out.println(arrayList); // [3,1,2] (Insertion order)
Set<Integer> treeSet = new TreeSet<>(List.of(3,1,2));
System.out.println(treeSet); // [1,2,3] (Natural order)
Map<Integer, String> linkedMap = new LinkedHashMap<>();
linkedMap.put(3,"C"); linkedMap.put(1,"A");
System.out.println(linkedMap); // {3=C, 1=A} (Insertion order)
// Different collection types maintain different orders
- Tip: Use LinkedHashSet for insertion-order uniqueness
- Real Use: Audit logs (order-critical) vs Dictionary (sorted)
4. Time Complexity Comparison
List<Integer> list = new ArrayList<>();
list.contains(5); // O(n) linear search
Set<Integer> set = new HashSet<>();
set.contains(5); // O(1) hash-based lookup
Map<Integer, String> map = new HashMap<>();
map.containsKey(5); // O(1) hash-based key search
// Sets and Maps outperform Lists for search operations
list.contains(5); // O(n) linear search
Set<Integer> set = new HashSet<>();
set.contains(5); // O(1) hash-based lookup
Map<Integer, String> map = new HashMap<>();
map.containsKey(5); // O(1) hash-based key search
// Sets and Maps outperform Lists for search operations
- Tip: Prefer HashSet over ArrayList when frequent contains() checks are needed
- Real Use: User session tracking (fast lookup) vs Transaction history (sequential access)
5. Thread-Safe Alternatives
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
Set<String> concurrentSet = ConcurrentHashMap.newKeySet();
Map<String, Integer> concurrentMap = new ConcurrentHashMap<>();
// Safe for multi-threaded environments
// Standard implementations aren't thread-safe by default
Set<String> concurrentSet = ConcurrentHashMap.newKeySet();
Map<String, Integer> concurrentMap = new ConcurrentHashMap<>();
// Safe for multi-threaded environments
// Standard implementations aren't thread-safe by default
- Tip: Use CopyOnWriteArrayList for read-heavy concurrent scenarios
- Real Use: Real-time stock tickers (high concurrency)
💡 Pro Interview Tip
When asked about collections, always mention: 1) Order guarantees 2) Null handling 3) Thread safety 4) Time complexities. Example: "HashMap offers O(1) lookups but isn't thread-safe, whereas Hashtable is synchronized but slower."