Streams & LambdasBeginner
1Question 1 of 1
javaHow do Java Streams and Lambda Expressions simplify functional data processing?
Explanation & Concept
Introduced in Java 8, Streams provide a declarative pipeline for processing sequences of elements. Streams do not store elements; they carry values from a data source through computational steps.
Stream operations are divided into: 1. Intermediate operations (lazy): `filter()`, `map()`, `sorted()`, `distinct()`. 2. Terminal operations (eager): `collect()`, `forEach()`, `reduce()`, `count()`.
Code Example
java
import java.util.List;
import java.util.stream.Collectors;
public class StreamPipeline {
record Employee(String name, String department, double salary) {}
public static void main(String[] args) {
List<Employee> staff = List.of(
new Employee("Alice", "Engineering", 120000),
new Employee("Bob", "Marketing", 75000),
new Employee("Charlie", "Engineering", 140000)
);
List<String> highEarners = staff.stream()
.filter(e -> e.department().equals("Engineering"))
.filter(e -> e.salary() > 100000)
.map(Employee::name)
.sorted()
.collect(Collectors.toList());
System.out.println("High earning engineers: " + highEarners); // [Alice, Charlie]
}
}How It Works & Key Takeaways
Because intermediate operations like `filter` and `map` are evaluated lazily, elements are processed in a single pass without allocating intermediate collection buffers.
Mock Test · 1 Questions · Optional
Ready for OOP & Interfaces?
Take a quick test to evaluate yourself, or move straight to the next module.
Beginner1 questions