Last active
December 11, 2021 07:04
-
-
Save codinko/6af4e26b1e97e33c7d309541c84f1946 to your computer and use it in GitHub Desktop.
FindEmployeesWithSameSalary Java 8 Streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.learn.java; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
public class FindEmployeesWithSameSalary { | |
public static void main(String[] args) { | |
List<Employee> employees = new ArrayList<>(); | |
employees.add(new Employee(1, "John" , 1000)); | |
employees.add(new Employee(1, "Peter" , 2000)); | |
employees.add(new Employee(1, "Ben" , 3000)); | |
employees.add(new Employee(1, "Steve" , 2000)); | |
employees.add(new Employee(1, "Parker" , 1000)); | |
Map<Integer, List<Employee>> map1 = employees.stream() | |
.collect(Collectors.groupingBy(Employee::getSalary, Collectors.toList())); | |
System.out.println("map1 :: "+ map1); | |
Map<Integer, List<Employee>> map2 = employees.stream() | |
.collect(Collectors.groupingBy(Employee::getSalary)); | |
System.out.println("map2 :: "+ map1); | |
Map<Integer, Set<String>> map3 = employees.stream() | |
.collect(Collectors.groupingBy | |
(Employee::getSalary, Collectors.mapping | |
(Employee::getName, Collectors.toSet()))); | |
System.out.println("map3 :: "+ map3); | |
map3.forEach((k,v) -> { | |
if(v.size()>1) { | |
System.out.println("salary :: "+ k + " is same for " + v); | |
} | |
}); | |
} | |
} | |
----- | |
OUTPUT | |
map1 :: {2000=[com.learn.java.Employee@6acbcfc0, com.learn.java.Employee@5f184fc6], 3000=[com.learn.java.Employee@3feba861], 1000=[com.learn.java.Employee@5b480cf9, com.learn.java.Employee@6f496d9f]} | |
map2 :: {2000=[com.learn.java.Employee@6acbcfc0, com.learn.java.Employee@5f184fc6], 3000=[com.learn.java.Employee@3feba861], 1000=[com.learn.java.Employee@5b480cf9, com.learn.java.Employee@6f496d9f]} | |
map3 :: {2000=[Steve, Peter], 3000=[Ben], 1000=[Parker, John]} | |
salary :: 2000 is same for [Steve, Peter] | |
salary :: 1000 is same for [Parker, John] | |
--- | |
Employee.java | |
package com.learn.java; | |
public class Employee { | |
public Employee(int id, String name, int salary) { | |
this.id = id; | |
this.name = name; | |
this.salary = salary; | |
} | |
private int id; | |
private String name; | |
private int salary; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getSalary() { | |
return salary; | |
} | |
public void setSalary(int salary) { | |
this.salary = salary; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment