Answer:
You can use Collections.groupBy() to group list of employees by employee name.
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class MaximumUsingStreamMain { public static void main(String args[]) { List<Employee> employeeList = createEmployeeList(); Map<String, List<Employee>> map = employeeList.stream() .collect(Collectors.groupingBy(Employee::getName)); map.forEach((name,employeeListTemp)->System.out.println("Name: "+name+" ==>"+employeeListTemp)); } public static List<Employee> createEmployeeList() { List<Employee> employeeList=new ArrayList<>(); Employee e1=new Employee("John",21); Employee e2=new Employee("Martin",19); Employee e3=new Employee("Mary",31); Employee e4=new Employee("Mary",18); Employee e5=new Employee("John",26); employeeList.add(e1); employeeList.add(e2); employeeList.add(e3); employeeList.add(e4); employeeList.add(e5); return employeeList; } }
Output:
Name: John ==>[Employee Name: John age: 21, Employee Name: John age: 26]Name: Martin ==>[Employee Name: Martin age: 19]Name: Mary ==>[Employee Name: Mary age: 31, Employee Name: Mary age: 18]
Don't miss the next article!
Be the first to be notified when a new article or Kubernetes experiment is published.
Share This