Java Collections Framework Cheat Sheet
Covers List, Set, and Map basics, choosing between implementations, sorting with Comparator, and immutable collection factories.
2 PagesIntermediateApr 5, 2026
List, Set, and Map Basics
The three core collection types and their fundamental operations.
java
List<String> list = new ArrayList<>();list.add("a");list.add("b");list.get(0); // "a" - indexed access, O(1) for ArrayListlist.remove("a");Set<String> set = new HashSet<>();set.add("x");set.add("x"); // ignored - duplicates not allowedSystem.out.println(set.size()); // 1Map<String, Integer> map = new HashMap<>();map.put("age", 30);map.getOrDefault("height", 0); // 0 - safe default lookupmap.forEach((k, v) -> System.out.println(k + "=" + v));
Choosing Implementations
Trade-offs between the common List, Set, and Map implementations.
java
List<Integer> arrayList = new ArrayList<>(); // fast random access, slow middle insertsList<Integer> linkedList = new LinkedList<>(); // fast insert/remove at ends, slow random accessSet<Integer> hashSet = new HashSet<>(); // O(1) avg, no order guaranteeSet<Integer> linkedHashSet = new LinkedHashSet<>(); // preserves insertion orderSet<Integer> treeSet = new TreeSet<>(); // sorted, O(log n)Map<String, Integer> hashMap = new HashMap<>(); // no order guaranteeMap<String, Integer> linkedHashMap = new LinkedHashMap<>(); // insertion orderMap<String, Integer> treeMap = new TreeMap<>(); // sorted by key
Iteration & Sorting
Sort collections and safely remove items while iterating.
java
List<String> names = new ArrayList<>(List.of("Charlie", "Alice", "Bob"));Collections.sort(names); // natural ordering (alphabetical)names.sort(Comparator.reverseOrder()); // descendingnames.sort(Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder()));for (String name : names) { // enhanced for-loop (uses Iterator internally) System.out.println(name);}Iterator<String> it = names.iterator();while (it.hasNext()) { if (it.next().equals("Bob")) it.remove(); // safe removal during iteration}
Core Interfaces & Complexity
Which interface to reach for, and its typical performance profile.
- Collection- Root interface for List, Set, and Queue
- List- Ordered, allows duplicates; implementations: ArrayList, LinkedList, Vector
- Set- No duplicates; implementations: HashSet, LinkedHashSet, TreeSet
- Map- Key-value pairs, not a Collection subtype; implementations: HashMap, TreeMap, LinkedHashMap
- Queue / Deque- FIFO/double-ended access; implementations: ArrayDeque, LinkedList, PriorityQueue
- ArrayList get/add(end)- O(1) amortized; add/remove in the middle is O(n)
- HashMap get/put- O(1) average case, O(n) worst case on hash collisions
- TreeMap/TreeSet- O(log n) operations, keeps elements sorted via Comparable or Comparator
Immutable Collections & Utilities
Create read-only collections and use the Collections helper class.
java
List<String> immutable = List.of("a", "b", "c"); // Java 9+, throws on mutationMap<String, Integer> immutableMap = Map.of("x", 1, "y", 2);List<String> unmodifiable = Collections.unmodifiableList(list); // read-only viewCollections.max(list);Collections.reverse(list);Collections.emptyList();
Pro Tip
Always override both equals() and hashCode() together when using custom objects as HashMap/HashSet keys - inconsistent implementations silently break lookups because the object may hash into the wrong bucket.
Was this cheat sheet helpful?
Explore Topics
#JavaCollectionsFramework#JavaCollectionsFrameworkCheatSheet#Programming#Intermediate#ListSetAndMapBasics#ChoosingImplementations#IterationSorting#CoreInterfacesComplexity#DataStructures#Algorithms#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance