Source
Creates a Stream from a Collection
Intermediate
Lazy, Returns a Stream, Chainable
Terminal
Triggers Execution, Ends the Stream
Collectors
Reduces Stream into List / Set / Map
Lazy
→
Intermediate Ops Don't Run Until Terminal Op Called
Once
→
A Stream Can Be Consumed Only Once
Immutable
→
Stream Never Modifies the Source Collection
Highest Salary Employee
employees.stream()
.max(Comparator.comparing(Employee::getSalary));
Second Highest Salary
employees.stream()
.sorted(Comparator.comparing(Employee::getSalary).reversed())
.skip(1)
.findFirst();
Group Employees by Department
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
Count Employees by Department
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
Average Salary by Department
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.averagingDouble(Employee::getSalary)));
Find Duplicate Elements
Set<Integer> seen = new HashSet<>();
list.stream()
.filter(n -> !seen.add(n))
.collect(Collectors.toList());
Employees with Salary > 50000
employees.stream()
.filter(e -> e.getSalary() > 50000)
.collect(Collectors.toList());
Sort Employees by Salary (Ascending)
employees.stream()
.sorted(Comparator.comparing(Employee::getSalary))
.collect(Collectors.toList());
Highest Paid Employee per Department
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.maxBy(Comparator.comparing(Employee::getSalary))));
Employee Names to Uppercase
employees.stream()
.map(e -> e.getName().toUpperCase())
.collect(Collectors.toList());
Sum of Salary by Department
list.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingDouble(Employee::getSalary)));
Salary > 20000 AND Gender Male
list.stream()
.filter(e -> e.getSalary() > 20000.00)
.filter(e -> e.getGender().equalsIgnoreCase("Male"))
.collect(Collectors.toList());
Duplicate Employee Names ⭐ ⭐
employees.stream()
.collect(Collectors.groupingBy(Employee::getName, Collectors.counting()))
.entrySet().stream()
.filter(en -> en.getValue() > 1)
.map(Map.Entry::getKey) //.map(e -> e.getKey())
.collect(Collectors.toSet());
First Non-Repeating Character | String str = "swiss ⭐ ⭐
str.chars().mapToObj(c -> (char) c)
.collect(Collectors.groupingBy( c -> c, LinkedHashMap::new,Collectors.counting()))
.entrySet().stream()
.filter(e -> e.getValue() == 1)
.map(Map.Entry::getKey)
.findFirst();
Names Starting with "C" → Uppercase
names.stream()
.filter(a -> a.startsWith("C"))
.map(String::toUpperCase)
.collect(Collectors.toList());
Remove Duplicates via distinct()
List<Integer> uniqueList = list.stream()
.distinct()
.collect(Collectors.toList());
✓Stream introduced in Java 8
✓Stream does not store data
✓Stream does not modify original collection
✓Intermediate Operations are Lazy
✓Terminal Operation triggers execution
✓Stream can be consumed only once
✓map() = Transform Data (1-to-1)
✓flatMap() = Flatten Nested Collections
✓filter() = Apply Condition
✓reduce() = Aggregation
✓collect() = Final Result
✓groupingBy() = Group Data
✓counting() = Count Grouped Data
✓averagingDouble() = Calculate Average
✓partitioningBy() = Split into TRUE/FALSE
✓parallelStream() = Parallel Processing