May 4, 2025

🧩 Java Collections Cheat Sheet: List vs Set vs Map — And All Their Variants

Java’s Collection Framework is powerful — but choosing the right data structure can be confusing. This blog covers all major types: List, Set, Map, and their key implementations like ArrayList, HashSet, HashMap, etc. Let’s break them down.


📦 1. Collection Hierarchy Overview

               Collection
              /    |     \
           List   Set    Queue
                    \
                    Map (not a Collection, but part of framework)

🔢 List — Ordered, Duplicates Allowed

✅ Common Implementations:

Type Preserves Order Allows Duplicates Thread-Safe Random Access Best For
ArrayList ✅ Yes ✅ Yes ❌ No ✅ Fast (O(1)) Fast access, rare insert/delete
LinkedList ✅ Yes ✅ Yes ❌ No ❌ Slow (O(n)) Frequent insertions/deletions
Vector ✅ Yes ✅ Yes ✅ Yes ✅ Fast (O(1)) Thread-safe, legacy code

🚫 Set — No Duplicates Allowed

✅ Common Implementations:

Type Preserves Order Sorted Allows Duplicates Thread-Safe Best For
HashSet ❌ No ❌ No ❌ No ❌ No Fast insert/check, unordered
LinkedHashSet ✅ Yes ❌ No ❌ No ❌ No Insertion order preserved
TreeSet ✅ Yes (sorted) ✅ Yes ❌ No ❌ No Sorted unique elements

🔎 TreeSet uses a Red-Black Tree (log(n) operations).


🗺️ Map — Key-Value Pairs (Keys Unique)

✅ Common Implementations:

Type Order Sorted Allows Nulls Thread-Safe Best For
HashMap ❌ No ❌ No ✅ One null key, many null values ❌ No Fast lookup by key (O(1) avg)
LinkedHashMap ✅ Insertion order ❌ No ✅ Same as HashMap ❌ No Ordered key-value pairs
TreeMap ✅ Sorted by keys ✅ Yes ❌ No null keys ❌ No Sorted map, navigation methods
Hashtable ❌ No ❌ No ❌ No null key/value ✅ Yes Legacy thread-safe map
ConcurrentHashMap ❌ No ❌ No ❌ No null key/value ✅ High performance Concurrent access

💡 HashMap vs TreeMap vs LinkedHashMap

Feature HashMap TreeMap LinkedHashMap
Lookup Time O(1) average O(log n) O(1)
Key Order None Sorted (natural or comparator) Insertion order
Null Keys ✅ One allowed ❌ Not allowed ✅ One allowed
Use When Fast access Sorted keys needed Maintain order

🧠 When to Use What?

Use Case Recommended Class
Fast search by key HashMap
Preserve insertion order (Map) LinkedHashMap
Maintain sorted key-value pairs TreeMap
Unique values only HashSet
Sorted unique values TreeSet
Ordered unique values LinkedHashSet
Thread-safe Map (modern) ConcurrentHashMap
Fast element access ArrayList
Many insertions/deletions LinkedList

🔐 Thread-Safety Tips

  • Prefer ConcurrentHashMap over Hashtable.

  • For other collections, use:

    List<String> syncList = Collections.synchronizedList(new ArrayList<>());
    Set<String> syncSet = Collections.synchronizedSet(new HashSet<>());
    

⚙️ Bonus: How Growth Happens

Structure Growth Factor Notes
ArrayList 1.5x Internal array resized when full
Vector 2x Legacy; slower due to synchronization
HashMap ~2x Doubles capacity when load factor > 0.75

✅ Final Thoughts

Understanding the right collection for your use case can boost both performance and readability. Bookmark this post for quick reference and code like a pro!


Would you like this turned into a Markdown blog post, PDF, or HTML page for publishing?