Unit 2 · Software engineering · Lesson 10 of 15
Java essentials
Step 1: Learn the ideas
- Collections framework: List, Set, Map, Deque, PriorityQueue
- equals/hashCode contract and records
- Generics, wildcards and type erasure
- Immutability: final fields, unmodifiable collections, records
- java.util.concurrent: executors, futures, concurrent collections, atomics
- volatile, synchronized and happens-before
- Virtual threads (Java 21 and later)
- JVM heap, stacks and generational garbage collection
Why it matters
Choosing Java for an interview signals that you write production-style Java. The common failures are semantic rather than syntactic: a HashMap key whose hash changes after insertion, a counter made "thread-safe" with volatile, a generic method that assumes type information exists at runtime, or a cache that leaks because its entries stay reachable.
Core ideas
- Collections. Program to interfaces (
List,Map,Deque).ArrayListgives O(1) indexed access;ArrayDequeis the usual stack and queue;HashMaphas expected O(1) operations and no order;LinkedHashMapkeeps insertion or access order, which makes a simple LRU cache;TreeMapkeeps keys sorted with O(log n) operations and range queries;PriorityQueueis a binary heap ordered by natural order or a comparator. Iterators of the general-purpose implementations are fail-fast on a best-effort basis: a structural change during iteration usually throwsConcurrentModificationException. - equals and hashCode. Equal objects must have equal hash codes, and both should depend only on fields that do not change while the object sits in a hash-based collection.
==compares references, so compare boxed numbers and strings withequals. Records generate consistentequals,hashCodeandtoStringfrom their components. - Generics and erasure. Type arguments exist only at compile time; at runtime
List<String>andList<Integer>share one class. You cannot testinstanceof List<String>, createnew T[n], or overload methods that differ only in type arguments. Use? extends Tfor parameters you only read from and? super Tfor parameters you only write to. - Immutability.
finalstops reassignment of a field, not mutation of the object it refers to. Immutable classes (final fields, no setters, defensive copies, or records with immutable components) are easy to share between threads.List.ofbuilds an unmodifiable list;Collections.unmodifiableListis a read-only view of a list that can still change underneath. - Concurrency. Prefer executors to hand-managed threads,
CompletableFuturefor composing asynchronous results, andConcurrentHashMap,AtomicLongorLongAdderfor shared state. Since Java 21, virtual threads make thread-per-task code cheap for blocking I/O.volatileguarantees visibility and ordering of individual reads and writes but does not makecount++atomic;synchronizedand thejava.util.concurrentlocks provide mutual exclusion plus happens-before edges. - JVM memory. Objects live on the garbage-collected heap; each thread's stack holds frames with local primitives and references. HotSpot's collectors are generational because most objects die young, and G1 is selected by default on most configurations. A Java leak is an object that stays reachable but is no longer needed, such as entries in a static map or listeners never removed.
Common misconceptions
- "
volatilemakes increments thread-safe." Each read and write is visible, butcount++is still a read-modify-write race; use an atomic class or a lock. - "
finalmeans immutable." It fixes the reference, not the state of the referenced object. - "Generic types are checked at runtime." They are erased; raw types and unchecked casts can put a wrong element into a collection, which then fails with a ClassCastException far from the cause.
- "Garbage collection prevents memory leaks." It reclaims only unreachable objects; reachable but useless objects accumulate.
How to approach it
Write small programs that demonstrate each failure (a mutated HashMap key, a lost update under volatile) and then fix them. Mid-level and senior backend conversations may extend into thread-pool sizing, CompletableFuture error handling and garbage-collection pauses.
Suggested order
- 1Read the dev.java "The Collections Framework" tutorial and write a one-page table of the implementations above with ordering guarantees and operation costs.
- 2Read the equals and hashCode entries of the java.lang.Object API page; use a class with a mutable field as a HashMap key, mutate it after insertion, explain why lookup fails, then convert the class to a record.
- 3Read the dev.java "Type Erasure" page; explain why new T[n] and instanceof List<String> do not compile, and write one method each with ? extends T and ? super T.
- 4Read the "High Level Concurrency Objects" part of the Concurrency tutorial and the java.util.concurrent package summary; rewrite a raw-thread program with an ExecutorService and CompletableFuture, including shutdown.
- 5Read section 17.4 (Memory Model) of JLS chapter 17 for the happens-before rules of volatile, locks and thread start/join; skip the formal execution model.
- 6Read "Garbage Collector Implementation" and "Available Collectors" in the HotSpot GC Tuning Guide (Release 25).
- 7Solve LRU Cache with LinkedHashMap and again with a HashMap plus doubly linked list; solve Print FooBar Alternately and Building H2O with java.util.concurrent primitives.
Step 2: Check off what you can do
Tap each one you can genuinely do or explain. Each earns 10 XP.
Step 3: Go deeper
Dev.java: Learn JavaStart here
Oracle (dev.java)Section: The Collections Framework
Collection interfaces and implementations; continue with the Type Erasure, Records and Virtual Threads tutorials listed under this resource.
Open(opens in a new tab)Link responded, 5 Oct 2026Java SE 25 API documentation
Oracle (docs.oracle.com)Section: java.lang.Object: equals and hashCode
The exact equals and hashCode contracts; the java.util.concurrent package summary for executors, synchronizers and memory-consistency rules.
Open(opens in a new tab)Link responded, 5 Oct 2026The Java Tutorials: Concurrency lesson
Oracle (docs.oracle.com)Section: High Level Concurrency Objects
Concepts behind lock objects, executors, concurrent collections and atomic variables (written for JDK 8).
Open(opens in a new tab)Link responded, 5 Oct 2026The Java Language Specification, Java SE 25 Edition: Chapter 17, Threads and Locks
Oracle (docs.oracle.com)Section: 17.4. Memory Model
Happens-before rules for volatile, locks, thread start and join; read the informal parts and examples.
Open(opens in a new tab)Link responded, 5 Oct 2026HotSpot Virtual Machine Garbage Collection Tuning Guide (Java SE 25)
Oracle (docs.oracle.com)Section: Garbage Collector Implementation
Generations and the weak generational hypothesis; then Available Collectors for when Serial, Parallel, G1 or ZGC fits.
Open(opens in a new tab)Link responded, 5 Oct 2026
Step 4: Practice
Write every solution in Java and use the language deliberately: LinkedHashMap in access order for LRU Cache (then a hand-built version), a bucket array whose keys rely on equals and hashCode for Design HashMap, PriorityQueue with a comparator for Top K, and Semaphore, ReentrantLock with Condition, or CyclicBarrier for the concurrency problems. Extra exercise outside LeetCode: write an immutable value class and the equivalent record, and test both as HashMap keys and across threads.
- LRU CacheLeetCode(opens in a new tab)Not attemptedLeetCode
- Design HashMapLeetCode(opens in a new tab)Not attemptedLeetCode
- Top K Frequent ElementsLeetCode(opens in a new tab)Not attemptedLeetCode
- Print FooBar AlternatelyLeetCode(opens in a new tab)Not attemptedLeetCode
- Building H2OLeetCode(opens in a new tab)Not attemptedLeetCode
- The Dining PhilosophersLeetCode(opens in a new tab)Not attemptedLeetCode
Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).
Step 5: Test yourself
Answer each one out loud or on paper before you finish the lesson.
- Question 1When would you choose TreeMap over HashMap?
- Question 2What goes wrong if two equal objects have different hash codes?
- Question 3Why can you not write new T[10] in a generic class?
- Question 4What does ? super T allow you to do with a parameter?
- Question 5Is a final List<String> field immutable?
- Question 6What makes an object eligible for garbage collection?
You’re done when you can
- Explain the equals/hashCode contract and demonstrate the mutated-key bug in a short program.
- Explain type erasure and two compile errors it causes, without notes.
- Rewrite a thread-based program with an ExecutorService and explain how its tasks are shut down.
- Explain what volatile guarantees and why it does not make an increment atomic.


