| Need | Type | Notes |
|---|---|---|
| Indexed list | ArrayList | Default. Amortized O(1) append. |
| Deque / stack | ArrayDeque | No nulls. Prefer over LinkedList/Stack. |
| Unique, unordered | HashSet | equals/hashCode on elements. |
| Unique, insert order | LinkedHashSet | Stable iteration. |
| Unique, sorted | TreeSet | compareTo; no null unless comparator allows. |
| Enum set | EnumSet | Bit-vector fast path. |
| Key/value | HashMap | One null key. Not thread-safe. |
| Key/value + order | LinkedHashMap | Access-order mode can build an LRU. |
| Sorted map | TreeMap | O(log n). |
| Shared map | ConcurrentHashMap | No nulls. Weakly consistent iterators. |
| Rare write, many read | CopyOnWriteArrayList | Listeners — not a general list. |
List.of("a", "b"); // unmodifiable, no null, Java 9
Set.of("a");
Map.of("k", 1); // up to 10 pairs
Map.ofEntries(Map.entry("k", 1));
List.copyOf(mutable); // snapshot, rejects null
Collections.emptyList(); // singleton empty
Collections.singletonList(x);
Collections.unmodifiableList(live); // view — live still mutates
add on a factory list throws UnsupportedOperationException. stream().toList() (Java 16) is unmodifiable.
map.getOrDefault(k, 0);
map.putIfAbsent(k, v);
map.computeIfAbsent(k, key -> new ArrayList<>()).add(item);
map.computeIfPresent(k, (key, v) -> v + 1);
map.merge(k, 1, Integer::sum);
map.replaceAll((k, v) -> v.strip());
map.forEach((k, v) -> { });
for (var e : map.entrySet()) { e.getKey(); e.getValue(); }
HashMap: get == null means missing or a null value. ConcurrentHashMap: null means missing.
for (String s : list) { }
list.forEach(this::use);
list.removeIf(String::isBlank);
Iterator<String> it = list.iterator();
while (it.hasNext()) {
if (drop(it.next())) it.remove();
}
Do not list.remove inside for-each on ArrayList — fail-fast ConcurrentModificationException. That exception is not a concurrency tool.
List<String> xs = Arrays.asList("a", "b"); // fixed size view of an array
List<String> ys = new ArrayList<>(xs); // real list
String[] a = list.toArray(String[]::new); // Java 11
int[] raw = {1, 2, 3}; // primitive — not List<Integer>
IntStream.of(raw).boxed().toList();
Arrays.asList rejects add/remove (fixed size) but set writes through to the array. Nulls allowed. Prefer List.of when you want a true unmodifiable copy.
copyOf; view with unmodifiable*. Know the difference.~ Good luck with your revision! ~