JAVA — CHAPTER 12

Generic Collections · Concept Cheat Sheet
Wrapper Classes Boxing/Unboxing List / Set / Map PriorityQueue Immutable Factories Concurrent Collections
1 COLLECTIONS FRAMEWORK — WHY GENERICS?
List<String> names = new ArrayList<>(); names.add("Java"); // names.add(42); -> COMPILE ERROR, type-safe!

What generics give collections

  • The <Type> tells the compiler what's stored — catches type errors early
  • No manual casting needed when retrieving elements
  • Same collection classes work for any object type
2 TYPE-WRAPPER CLASSES & BOXING/UNBOXING
PrimitiveWrapper
intInteger
doubleDouble
booleanBoolean
charCharacter

Boxing / Unboxing

  • Collections can only store objects — not primitives
  • Autoboxing: Integer i = 5; (int → Integer, automatic)
  • Unboxing: int n = i; (Integer → int, automatic)
3 LISTS & COMMON Collections METHODS
List<Integer> nums = new ArrayList<>(List.of(5,3,8,1)); Collections.sort(nums); Collections.reverse(nums); int max = Collections.max(nums); Collections.shuffle(nums);

Key list implementations

  • ArrayList — fast random access, resizable array
  • LinkedList — fast insert/remove at ends
  • Iterate with a for-each loop or an Iterator
4 Queue & PriorityQueue
PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.offer(5); pq.offer(1); pq.offer(3); System.out.println(pq.poll()); // 1 — smallest first

Notes

Queue is FIFO by default; PriorityQueue instead always returns its smallest (or highest-priority, via a Comparator) element first from poll().

5 HASH TABLES, Set & Map

How a hash table works

Each key's hashCode() determines a "bucket" to store it in — giving average O(1) add/remove/lookup for HashSet/HashMap.

Set<String> ids = new HashSet<>(); // no duplicates Map<String,Integer> ages = new HashMap<>(); ages.put("Amit", 21); System.out.println(ages.get("Amit"));
6 IMMUTABLE COLLECTION FACTORY METHODS
List<Integer> nums = List.of(1, 2, 3); Set<String> ids = Set.of("a", "b"); Map<String,Integer> m = Map.of("x", 1, "y", 2); // nums.add(4); -> throws UnsupportedOperationException

Why use them

Quick, safe way to create a fixed, read-only collection — useful for constants or data that should never change after creation.

7 CONCURRENT COLLECTIONS

The problem

Standard collections (ArrayList, HashMap) are not safe when multiple threads modify them at once — can corrupt data or throw ConcurrentModificationException.

The fix

  • ConcurrentHashMap — thread-safe map
  • CopyOnWriteArrayList — thread-safe list for read-heavy use
  • Located in java.util.concurrent
8 METHOD REFERENCE TABLE

interface List<T>

MethodPurpose
add(item)append
get(i)read at index
remove(i)remove at index
indexOf(item)first index, or -1
size()element count

interface Set<T>

MethodPurpose
add(item)adds if not already present
contains(item)membership test
remove(item)removes by value
size()element count

interface Map<K,V>

MethodPurpose
put(k,v)insert/update a pair
get(k)value for key, or null
containsKey(k)true if key exists
keySet()/values()all keys / all values
remove(k)deletes the pair