Java Collections Framework Cheatsheet 2026
Complete handbook for Java collections: Lists, Sets, Maps, Queues, iteration styles, sorting techniques, thread-safe alternatives, and stream pipelines.
Interactive Skill Mastery
Mark commands as learned to build your customized reference tracker. Retained locally in this browser.
List Interfaces
When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
List<String> list = new ArrayList<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
List<String> list = new LinkedList<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
list.add("item");Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
list.get(index);Output Example
// Success (Operation executed on Java collection in JVM memory)Set Interfaces
When to Use
When storing elements that must be absolutely unique, automatically filtering out duplicate entries.
Common Mistakes
Assuming Set implementations preserve insertion order. Use LinkedHashSet if insertion order is required.
Shortcut / Pro-Tip
Sets are optimized for constant-time membership checking via '.contains()'.Example
Set<Integer> set = new HashSet<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing elements that must be absolutely unique, automatically filtering out duplicate entries.
Common Mistakes
Assuming Set implementations preserve insertion order. Use LinkedHashSet if insertion order is required.
Shortcut / Pro-Tip
Sets are optimized for constant-time membership checking via '.contains()'.Example
Set<Integer> set = new TreeSet<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
set.contains(item);Output Example
// Success (Operation executed on Java collection in JVM memory)Map Interfaces
When to Use
When establishing fast key-value lookups where each unique key maps to exactly one value.
Common Mistakes
Using custom classes as Map keys without overriding both 'hashCode()' and 'equals()' correctly, breaking retrieval lookup logic.
Shortcut / Pro-Tip
Use 'getOrDefault(key, defaultValue)' to retrieve values safely without triggering NullPointerExceptions.Example
Map<String, Integer> map = new HashMap<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When establishing fast key-value lookups where each unique key maps to exactly one value.
Common Mistakes
Using custom classes as Map keys without overriding both 'hashCode()' and 'equals()' correctly, breaking retrieval lookup logic.
Shortcut / Pro-Tip
Use 'getOrDefault(key, defaultValue)' to retrieve values safely without triggering NullPointerExceptions.Example
Map<String, Integer> map = new TreeMap<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
map.put("key", 100);Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
map.getOrDefault("key", 0);Output Example
// Success (Operation executed on Java collection in JVM memory)Queue & Deque
When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
Queue<String> queue = new LinkedList<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
Queue<Integer> pq = new PriorityQueue<>();Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
queue.poll();Output Example
// Success (Operation executed on Java collection in JVM memory)Collections Helpers
When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
Collections.sort(list);Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
Collections.reverse(list);Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
Collections.shuffle(list);Output Example
// Success (Operation executed on Java collection in JVM memory)Stream Pipelines
When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
list.stream().filter(x -> x.startsWith("A")).collect(Collectors.toList());Output Example
// Success (Operation executed on Java collection in JVM memory)When to Use
When storing, organizing, processing, or sorting groups of objects inside standard Java applications.
Common Mistakes
Instantiating structural interfaces directly (e.g., trying to write 'new List()') instead of instantiating concrete classes (e.g., 'new ArrayList()').
Shortcut / Pro-Tip
Use modern static factory methods like List.of() or Set.of() to initialize immutable collections in one line.Example
list.stream().map(String::toUpperCase).forEach(System.out::println);Output Example
// Success (Operation executed on Java collection in JVM memory)Java Collections Best Practices
1Code to Interfaces
Always declare variables using interfaces (e.g. List<String> list = new ArrayList<>()) instead of concrete classes, enabling painless implementation swaps.
2Pre-size Collections When Possible
Provide an initial capacity when creating ArrayLists or HashMaps if you know the approximate size to avoid frequent, expensive underlying array resizes.
3Use Collections.unmodifiableCollection()
Protect your internal application data structures by exposing read-only, unmodifiable wrappers to external clients.
4Choose the Correct Map Type
Use HashMap for general speed, LinkedHashMap to preserve insertion order, and TreeMap when keys must remain continuously sorted.
5Avoid Vector and Hashtable
These are legacy, synchronized classes with high performance overhead. Use ArrayList, HashMap, or modern java.util.concurrent concurrent classes.
Common Java Collections Errors & Solutions
ConcurrentModificationException
Modifying a collection structurally while iterating over it. Solution: Use Iterator.remove() or collection.removeIf() instead of standard loops.
NullPointerException in TreeSet/TreeMap
Attempting to insert a null key into sorted structures. Solution: Filter out nulls or supply a custom comparator that handles null values safely.
IndexOutOfBoundsException
Requesting a list index that is less than zero or greater than/equal to the list size. Solution: Check list sizes prior to indexing.
UnsupportedOperationException
Attempting to modify an immutable collection (like List.of() outputs). Solution: Wrap the immutable collection in a mutable container (e.g. new ArrayList<>(immutableList)).
ClassCastException inside Collections.sort()
Sorting a collection whose elements do not implement Comparable. Solution: Implement Comparable on your custom class or pass an explicit Comparator.
Common Java Collections Interview Questions
Q1What is the root interface of the Java Collections Framework?
The Collection interface is the root of the collection hierarchy, although Map is also part of the framework but does not inherit from Collection.
Q2What is the difference between List and Set?
A List is an ordered collection that allows duplicate elements. A Set is an unordered collection that contains no duplicate elements.
Q3How do HashMap and TreeMap differ?
HashMap provides constant-time performance (O(1)) for basic operations and does not guarantee element ordering. TreeMap guarantees that keys are sorted in natural or custom order, but has logarithmic time complexity (O(log n)).
Q4Why is it important to override hashCode() when overriding equals()?
If two objects are equal according to equals(), they must produce the identical integer result from hashCode(). Failing to do so breaks collection rules, preventing correct object retrieval in HashMaps or HashSets.
Q5What is the difference between fail-fast and fail-safe iterators?
Fail-fast iterators (e.g., ArrayList iterator) throw a ConcurrentModificationException if the collection is structurally modified during iteration. Fail-safe iterators (e.g., CopyOnWriteArrayList iterator) iterate over a copy and do not throw this.
Related Resources
REST API Tester
Test API routes and endpoints directly in your browser with full request controls.
JSON Formatter & Validator
Beautify, validate, and minify JSON structures instantly.
Best Free Online Developer Tools
An expert review of must-have online utilities for developers.
Git Cheatsheet
Essential command reference for local and remote version control repositories.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com