Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Modern Java

    Quick Reference

    Glossary and cheat sheet for Modern Java — the page to reread before an interview.

    Extra: Reference6 min read

    Glossary#

    TermPlain meaningChapter
    lambdaanonymous function (params) -> body1
    capture / effectively finalusing outer variables / never reassigned1
    functional interfaceinterface with one abstract method2
    Predicate / Function / Consumer / Supplier / BiFunction / UnaryOperator / BinaryOperatorT→bool / T→R / T→void / ()→T / (T,U)→R / T→T / (T,T)→T2
    method referenceClass::method shorthand for a one-call lambda3
    stream / pipelinesequence processed by chained operations / the chain4
    source / intermediate / terminalwhere data comes from / lazy steps / result-producing step4
    declarative vs imperativesay what vs say how4
    filter / map / flatMap / reducekeep / transform / transform-and-flatten / fold into one value5
    collector / groupingBy / partitioningBy / joining / toMappacking rule / by key / true-false split / string join / key→value (with merge)6
    lazy evaluation / short-circuitnothing runs until needed / stop once the answer is known7
    IntStream / boxed()primitive stream / back to Stream7
    parallel streamsplits work across cores via a shared pool7
    Optional / orElse / orElseGet / orElseThrowmaybe-a-value / eager default / lazy default / throw if empty8
    varinferred local variable type9
    text block""" multi-line string9
    switch expression / yieldswitch producing a value / value from a block case9
    sealed / permitsclosed set of subtypes / the list of them10
    type pattern / record pattern / guard (when)case T t / case R(a, b) / extra condition10
    exhaustive switchcompiler-verified coverage of all cases10
    expression problemnew types easy (polymorphism) vs new operations easy (pattern matching)10
    LTSlong-term support version: 8, 11, 17, 21, 2511

    Cheat Sheet#

    Lambdas and references

    Java
    x -> x * 2        (a, b) -> Integer.compare(a, b)        () -> new ArrayList<>()        s -> { ...; return v; }
    Student::name     Integer::parseInt     System.out::println     ArrayList::new     Integer::sum

    Stream recipes

    Java
    list.stream().filter(p).map(f).sorted(cmp).toList()
    list.stream().mapToInt(Student::score).sum() / .average().orElse(0) / .max().getAsInt()
    list.stream().collect(Collectors.groupingBy(Student::city, Collectors.counting()))
    list.stream().collect(Collectors.groupingBy(k, Collectors.mapping(v, Collectors.toList())))
    list.stream().collect(Collectors.toMap(k, v, (a, b) -> a))        // always give a merge function
    list.stream().map(String::valueOf).collect(Collectors.joining(","))
    nested.stream().flatMap(List::stream).distinct().toList()
    list.stream().anyMatch(p) / allMatch(p) / noneMatch(p)
    IntStream.range(0, n) / rangeClosed(1, n) · Arrays.stream(arr) · .boxed()

    Optional

    Java
    opt.map(f).orElse(constant)    opt.orElseGet(() -> costly())    opt.orElseThrow(() -> new NotFound(id))
    opt.ifPresent(v -> ...)        Optional.ofNullable(maybeNull)   // returns only — not fields/params

    Java 17–21

    Java
    sealed interface Event permits Paid, Refunded {}
    
    record Paid(int amount) implements Event {}
    
    record Refunded(int amount, String reason) implements Event {}
    
    String msg = switch (e) {
        case Paid p when p.amount() > 1000 -> "big payment";
        case Paid p -> "paid " + p.amount();
        case Refunded(int amt, String why) -> "refund " + amt + ": " + why;
    };

    Rules

    Output
      terminal op or nothing runs · streams single-use · no side effects in map/filter/forEach
      toList() is unmodifiable · toMap needs a merge fn · orElseGet for costly defaults
      parallel only for big, CPU-bound, stateless work — measure first
      loops over int[] for hot DSA code; streams for clarity

    Try This Yourself#

    1. Convert loops to streams. Take the StudyBuddy report from Java Basics Ch 9 (with your Student class from file 02's exercises) and compute, with streams only: the total, the average, the top student, the count per grade, and the passing students' names joined with ", ".

    2. Word frequency, streams edition. Given a paragraph, produce the 3 most frequent words using Arrays.stream(text.split("\\s+")), groupingBy(..., counting()), then sort the entries by count descending and limit(3). Compare it with the heap version from Collections, From the Inside Ch 9.

    3. Trace laziness. Add peek(x -> System.out.println("saw " + x)) at several points in a pipeline with sorted() in the middle. Explain why output before sorted appears all at once.

    4. Sealed events. Model StudyBuddy payment events (Paid, Refunded, Failed) as a sealed interface of records. Write describe(Event e) as a pattern switch. Then add a Disputed record to permits and watch the compiler point at the switch.