Employee Highest salary in each department using Java
Categories: Java Java Examples
Employee Highest salary in each department using Java
package r4r.co.in;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class EmployeeHighestSalaryInEachDepartment {
public static void main(String[] args) {
// Employee Highest salary in each department
List<Emp> list = new ArrayList<Emp>();
list.add(new Emp("Rajesh", 120, "IT"));
list.add(new Emp("Rajesh", 110, "IT"));
list.add(new Emp("Rajesh", 120, "ACT"));
list.add(new Emp("Rajesh", 110, "ACT"));
list.add(new Emp("Rajesh", 190, "OP"));
Map<Object, Object> map = list.stream().collect(Collectors.groupingBy(e -> e.getDep(), Collectors
.collectingAndThen(Collectors.maxBy(Comparator.comparingDouble(Emp::getSal)), Optional::get)));
System.out.println(map);
}
}
class Emp {
String name;
int sal;
String dep;
public Emp(String name, int sal, String dep) {
super();
this.name = name;
this.sal = sal;
this.dep = dep;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public String getDep() {
return dep;
}
public void setDep(String dep) {
this.dep = dep;
}
@Override
public String toString() {
return "Emp [name=" + name + ", sal=" + sal + ", dep=" + dep + "]";
}
}