Modern Java
Lambdas, streams, Optional, records, sealed types and pattern matching — the Java 8 to 21 features you will use every day.
- Module 8 of 11
- Intermediate
- 11 chapters
- 49 min
Modern Java code is written with lambdas, streams and Optional. These features make code shorter and safer, and interviewers expect you to read and use them.
What you will be able to do
- Write lambdas and method references
- Build stream pipelines with collectors
- Use Optional instead of null
- Model data with sealed types and pattern matching
Chapters
1. LambdasA lambda expression is an anonymous function: a piece of behaviour written inline and passed around as a value.5 min2. Functional InterfacesA functional interface is an interface with exactly one abstract method (default and static methods don't count). A lambda can be used4 min3. Method ReferencesInstead of writing a note that says "call the name method on whatever you're given", you just write the name of the method: "use name".2 min4. Streams: The IdeaA stream is a sequence of elements you process with a pipeline of operations. It does not store the elements — it describes a computation2 min5. Stream Operationsrecord Student(String name, int score, String city) {}5 min6. CollectorsThe packing station at the end of the belt: pack into one box, or into labelled boxes by city, or just count items per label, or string them3 min7. Laziness, Primitive Streams and Parallel StreamsIntermediate operations don't run until a terminal operation pulls elements through, and elements go through the pipeline one at a time, not4 min8. Optional: "Maybe a Value" Without nullOptional<T> is a container that holds either one value or nothing. It's designed as a return type for methods that might not find a3 min9. Small but Everywherevar map — new HashMap<String, List<Integer>>(); // the compiler infers HashMap<String, List<Integer>>2 min10. Sealed Types and Pattern MatchingA sealed class or interface (Java 17) lists exactly which classes may extend or implement it, using permits. Nobody else can.5 min11. The Version MapInterviewers ask "what's new in Java X?" and real codebases run on the LTS versions. What arrived when:1 min
Extras
The Whole StoryThe whole module retold as one story — no new facts, a fast revision of Modern Java.RevisionInterview QuestionsReal interview questions on Modern Java, answered the way you would say them out loud.Interview prepQuick ReferenceGlossary and cheat sheet for Modern Java — the page to reread before an interview.Reference