You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Java has evolved rapidly since adopting a six-month release cadence in 2017. This lesson covers the most impactful features from Java 8 through Java 21, including lambdas, streams, records, sealed classes, pattern matching, and virtual threads.
A lambda is a concise way to represent a function (an implementation of a functional interface):
// Before lambdas
Comparator<String> cmp = new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.length() - b.length();
}
};
// With a lambda
Comparator<String> cmp = (a, b) -> a.length() - b.length();
// No parameters
Runnable r = () -> System.out.println("Running");
// One parameter (parentheses optional)
Consumer<String> greet = name -> System.out.println("Hello, " + name);
// Multiple parameters
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
// Multi-line body
Comparator<String> cmp = (a, b) -> {
int diff = a.length() - b.length();
return diff != 0 ? diff : a.compareTo(b);
};
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.