Java & Structures
Updated for 2026

Java Collections Framework Cheatsheet 2026

Complete handbook for Java collections: Lists, Sets, Maps, Queues, iteration styles, sorting techniques, thread-safe alternatives, and stream pipelines.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 19 Mastered (0%)

List Interfaces

List<String> list = new ArrayList<>();
BeginnerBasics
Create a dynamically resizable array list. Fast for lookups, slow for mid-inserts.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
List<String> list = new LinkedList<>();
BeginnerBasics
Create a doubly-linked list. Good for constant-time additions/removals but slower for random lookups.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
list.add("item");
BeginnerBasics
Append an element to the end of the list.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
list.get(index);
AdvancedPerformance
Retrieve the element at a specific index (constant time for ArrayList).

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)

Set Interfaces

Set<Integer> set = new HashSet<>();
BeginnerBasics
Create an unordered collection containing no duplicate elements. Constant-time operations.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
Set<Integer> set = new TreeSet<>();
BeginnerBasics
Create a set sorted in ascending natural order (or custom comparator order). Logarithmic time.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
set.contains(item);
BeginnerBasics
Verify the presence of an element inside the set.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)

Map Interfaces

Map<String, Integer> map = new HashMap<>();
BeginnerBasics
Create a key-value mapping with unique keys. Allows one null key.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
Map<String, Integer> map = new TreeMap<>();
BeginnerBasics
Create a key-value mapping sorted by the natural ordering of keys.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
map.put("key", 100);
BeginnerBasics
Associate a value with a specified key.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
map.getOrDefault("key", 0);
BeginnerBasics
Retrieve a mapped value or return the designated fallback if key is missing.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)

Queue & Deque

Queue<String> queue = new LinkedList<>();
BeginnerBasics
Create a standard FIFO queue.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
Queue<Integer> pq = new PriorityQueue<>();
BeginnerBasics
Create a sorted heap queue where the smallest/highest priority element is retrieved first.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
queue.poll();
BeginnerBasics
Retrieve and remove the head element of the queue, or return null if empty.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)

Collections Helpers

Collections.sort(list);
BeginnerBasics
Sort a list in ascending natural order (in-place modification).

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
Collections.reverse(list);
BeginnerBasics
Invert the sequential order of elements in a list.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
Collections.shuffle(list);
BeginnerBasics
Randomize the permutation of elements in the specified list.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)

Stream Pipelines

list.stream().filter(x -> x.startsWith("A")).collect(Collectors.toList());
BeginnerBasics
Filter list elements and collect the matching results into a new List.

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

Console / Terminal
// Success (Operation executed on Java collection in JVM memory)
list.stream().map(String::toUpperCase).forEach(System.out::println);
BeginnerBasics
Transform each element and print the resulting values.

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

Console / Terminal
// 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

Error

ConcurrentModificationException

Solution

Modifying a collection structurally while iterating over it. Solution: Use Iterator.remove() or collection.removeIf() instead of standard loops.

Error

NullPointerException in TreeSet/TreeMap

Solution

Attempting to insert a null key into sorted structures. Solution: Filter out nulls or supply a custom comparator that handles null values safely.

Error

IndexOutOfBoundsException

Solution

Requesting a list index that is less than zero or greater than/equal to the list size. Solution: Check list sizes prior to indexing.

Error

UnsupportedOperationException

Solution

Attempting to modify an immutable collection (like List.of() outputs). Solution: Wrap the immutable collection in a mutable container (e.g. new ArrayList<>(immutableList)).

Error

ClassCastException inside Collections.sort()

Solution

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.