A lambda is a short anonymous function: (a, b) -> a + b.
The Stream API processes collections in a pipeline:
filter(): keep matching items
map(): transform items
sorted(), limit(), distinct()
collect(Collectors.toList()) or toList(), sum(), count(), average()
💡 Key Takeaways: Java Lambdas & Streams
Mastering Java Lambdas & Streams helps you write clean and reliable Java code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Java Lambdas & Streams Code Example
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> marks = List.of(72, 95, 58, 88, 64);
List<Integer> passed = marks.stream()
.filter(m -> m >= 60)
.sorted(Comparator.reverseOrder())
.toList();
double avg = marks.stream().mapToInt(Integer::intValue).average().orElse(0);
System.out.println(passed);
System.out.println("Average: " + avg);
List.of("ali", "sara").forEach(n -> System.out.println(n.toUpperCase()));
}
}
Output
[95, 88, 72, 64]
Average: 75.4
ALI
SARA
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
Java Lambdas & Streams Practice Exercises
Check what you learned in “Java Lambdas & Streams”.