Modern Java 8+ Features
Lambdas, functional interfaces, streams, Optional and the date-time API from Java 8, plus var, records and modern switch from later versions. Every lesson traces a real program line by line.
Lambdas and Functional Interfaces
- Lambda Expressions: Pass behaviour around as a value. Replace a bulky anonymous class with a one-line lambda, and watch it run each time it is used.
- Functional Interfaces: Predicate, Function, Consumer, Supplier: Java's four everyday lambda types: test a value, transform it, use it, or make one. Combine them with andThen.
- Method References: When a lambda only calls an existing method, write Class::method instead. Four kinds: static, of the argument, of an object, and constructors.
- Default and Static Interface Methods: Since Java 8, interfaces can contain code: default methods that classes inherit or override, and static helper methods.
The Stream API
- Streams: filter, map, collect: Describe what you want from a list as a pipeline. Watch each price travel through filter and map one at a time, and see that nothing runs until collect.
- Streams: reduce, count, max, average: Boil a stream down to one value: a total with reduce, a count, the biggest value, and an average.
- Collectors: groupingBy, counting, joining: Collect a stream into a map of groups, count each group, or join strings with separators.
- New Collection Methods: removeIf, replaceAll, merge, forEach: Java 8 added lambda-friendly methods to lists and maps. Clean, transform and count with one call each.
Safer APIs
- Optional: No More null Surprises: Return Optional instead of null when a value might be missing, and handle both cases without a NullPointerException.
- The Date and Time API (java.time): LocalDate, Period and ChronoUnit replace the old Date class: clear, unchangeable values with readable methods.
Modern Java (10–21)
- var: Local Type Inference: Since Java 10, let the compiler work out a local variable's type. Less typing, same strong types.
- Records: Data Classes in One Line: A record declares its fields once and gets a constructor, accessors, equals, hashCode and toString for free.
- Switch Expressions and Pattern Matching: Modern Java: switch that returns a value with arrows (no break needed), and instanceof that checks and names a type in one step.