Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| lambda | anonymous function (params) -> body | 1 |
| capture / effectively final | using outer variables / never reassigned | 1 |
| functional interface | interface with one abstract method | 2 |
| Predicate / Function / Consumer / Supplier / BiFunction / UnaryOperator / BinaryOperator | T→bool / T→R / T→void / ()→T / (T,U)→R / T→T / (T,T)→T | 2 |
| method reference | Class::method shorthand for a one-call lambda | 3 |
| stream / pipeline | sequence processed by chained operations / the chain | 4 |
| source / intermediate / terminal | where data comes from / lazy steps / result-producing step | 4 |
| declarative vs imperative | say what vs say how | 4 |
| filter / map / flatMap / reduce | keep / transform / transform-and-flatten / fold into one value | 5 |
| collector / groupingBy / partitioningBy / joining / toMap | packing rule / by key / true-false split / string join / key→value (with merge) | 6 |
| lazy evaluation / short-circuit | nothing runs until needed / stop once the answer is known | 7 |
| IntStream / boxed() | primitive stream / back to Stream | 7 |
| parallel stream | splits work across cores via a shared pool | 7 |
| Optional / orElse / orElseGet / orElseThrow | maybe-a-value / eager default / lazy default / throw if empty | 8 |
| var | inferred local variable type | 9 |
| text block | """ multi-line string | 9 |
| switch expression / yield | switch producing a value / value from a block case | 9 |
| sealed / permits | closed set of subtypes / the list of them | 10 |
type pattern / record pattern / guard (when) | case T t / case R(a, b) / extra condition | 10 |
| exhaustive switch | compiler-verified coverage of all cases | 10 |
| expression problem | new types easy (polymorphism) vs new operations easy (pattern matching) | 10 |
| LTS | long-term support version: 8, 11, 17, 21, 25 | 11 |
Cheat Sheet#
Lambdas and references
x -> x * 2 (a, b) -> Integer.compare(a, b) () -> new ArrayList<>() s -> { ...; return v; }
Student::name Integer::parseInt System.out::println ArrayList::new Integer::sumStream recipes
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
opt.map(f).orElse(constant) opt.orElseGet(() -> costly()) opt.orElseThrow(() -> new NotFound(id))
opt.ifPresent(v -> ...) Optional.ofNullable(maybeNull) // returns only — not fields/paramsJava 17–21
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
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 clarityTry This Yourself#
Convert loops to streams. Take the StudyBuddy report from Java Basics Ch 9 (with your
Studentclass 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 ", ".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 andlimit(3). Compare it with the heap version from Collections, From the Inside Ch 9.Trace laziness. Add
peek(x -> System.out.println("saw " + x))at several points in a pipeline withsorted()in the middle. Explain why output beforesortedappears all at once.Sealed events. Model StudyBuddy payment events (
Paid,Refunded,Failed) as a sealed interface of records. Writedescribe(Event e)as a pattern switch. Then add aDisputedrecord topermitsand watch the compiler point at the switch.