Skip to content

jugalkrajput/Day-22-Java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“˜ Day 22 – Optional, Method References & Advanced Streams

βœ… What I learned today

  1. Optional Class

  • Container object to avoid null checks

  • Optional.ofNullable(), ifPresent(), orElse(), orElseThrow()

//Code

Optional opt = Optional.ofNullable(getName()); String name = opt.orElse("Guest");

  1. Method References (::)

Shortcut for lambdas

Types: Static (Math::max), Instance (System.out::println), Constructor (Employee::new)

  1. Advanced Streams – Grouping

  • Collectors.groupingBy(Function) β†’ groups into Map<K, List>

  • Collectors.groupingBy(Function, downstream collector) β†’ group + count/average

//Code

// Group employees by department

Map<String, List> byDept = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment));

// Count per department

Map<String, Long> counts = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));

// Average salary per department Map<String, Double> avgSalary = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.averagingDouble(Employee::getSalary)));

About

Today I have learn about Java 8 Features.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages