
A Complete Guide to Collectors in Java 8 Streams - Part 2
In the last part we saw, What is a Collector? How collect() Works Internally Commonly Used Built-in Collectors Grouping and Partitioning Now we will continue and take a dive into Downstream Collectors (Advanced) collectingAndThen() Creating a custom collector Parallel streams and collectors And more Downstream Collectors (Advanced) Collectors can be chained. Example: Group by department and calculate average salary. Map < String , Double > avgSalary = employees . stream () . collect ( Collectors . groupingBy ( Employee: : getDepartment , Collectors . averagingInt ( Employee: : getSalary ) )); Assuming we have the following employee list List < Employee > employees = Arrays . asList ( new Employee ( "Amit" , "IT" , "Developer" , 60000 ), new Employee ( "Neha" , "IT" , "Tester" , 50000 ), new Employee ( "Raj" , "HR" , "Recruiter" , 40000 ), new Employee ( "Simran" , "HR" , "Manager" , 70000 ), new Employee ( "Karan" , "Sales" , "Executive" , 45000 ) ); Output { IT=55000.0, HR=55000.0, Sa
Continue reading on Dev.to
Opens in a new tab




