fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone
  8. {
  9. public static void main(String[] args) {
  10.  
  11. List<Employee> employeeList = Arrays.asList(new Employee(22, "Rajan Anand", "Engineering", 1600000l),
  12. new Employee(23, "Swati Patil", "Testing", 800000l),
  13. new Employee(27, "Vijay Chawda", "Engineering", 800000l),
  14. new Employee(29, "Basant Mahapatra", "Engineering", 600000l),
  15. new Employee(32, "Ajay Patel", "Testing", 350000l));
  16.  
  17.  
  18. Map<String, Optional<Integer>> retVal = employeeList.stream()
  19. .collect(Collectors.groupingBy(Employee::getDepartment,
  20. Collectors.collectingAndThen(Collectors
  21. .maxBy(Comparator.comparing(Employee::getSalary)),
  22. e -> e.map(Employee::getEmployeeId))));
  23.  
  24. System.out.println(retVal);
  25.  
  26. }
  27.  
  28.  
  29. static class Employee {
  30. private int employeeId;
  31. private String employeeName;
  32. private String department;
  33. private Long salary;
  34.  
  35.  
  36. public Employee(int employeeId, String employeeName, String department, Long salary) {
  37. this.employeeId = employeeId;
  38. this.employeeName = employeeName;
  39. this.department = department;
  40. this.salary = salary;
  41. }
  42.  
  43.  
  44. public int getEmployeeId() {
  45. return employeeId;
  46. }
  47.  
  48. public String getEmployeeName() {
  49. return employeeName;
  50. }
  51.  
  52. public String getDepartment() {
  53. return department;
  54. }
  55.  
  56. public Long getSalary() {
  57. return salary;
  58. }
  59. }
  60. }
Success #stdin #stdout 0.13s 35064KB
stdin
Standard input is empty
stdout
{Engineering=Optional[22], Testing=Optional[23]}