Commit 6233967f authored by Uma Vani Ravuri's avatar Uma Vani Ravuri

Replace TestEmployee.java

parent 01c110e6
package com.java8.practies;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
......@@ -9,6 +10,7 @@ import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Collection;
public class TestEmployee {
public static void main(String[] args) {
......@@ -92,14 +94,25 @@ public class TestEmployee {
listEmp.stream().filter(x -> !x.getDepartmentNames().stream().equals("EEE"))
.collect(Collectors.toList()).forEach(System.out::println);
//given a list of employees. Can you create a map with the count of employees each department has ?
//with key as department name and count of employees as value.
Stream<String> distinctDept=listEmp.stream()
.map(Employee::getDepartmentNames)
.flatMap(x->x.stream());
.flatMap(Collection::stream);
Map<String, Long> mapDepartment = distinctDept.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
mapDepartment.forEach((k, v) -> System.out.println((k + ":" + v)));
//Given a map with the department name as key and value as list of employees belonging to that department.
Map<List<String>, List<Employee>> mapEmployee = listEmp.stream()
.collect(Collectors.groupingBy(Employee::getDepartmentNames, Collectors.toList()));
mapEmployee.forEach((k, v) -> System.out.println((k + ":" + v)));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment