fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Employee {
  8. public Employee(double salary) {
  9. this.salary = salary;
  10. }
  11.  
  12. private double salary;
  13.  
  14. public double getSalary() {
  15. return salary;
  16. }
  17.  
  18. public String toString() {
  19. return Integer.toHexString(hashCode()) + " " + String.valueOf(salary);
  20. }
  21. }
  22.  
  23. class Main {
  24. public static void main (String[] args) throws Exception {
  25. List<Employee> employees = Arrays.asList(
  26. new Employee(100),
  27. new Employee(150),
  28. new Employee(200),
  29. new Employee(150),
  30. new Employee(100)
  31. );
  32. double sum = 0;
  33. NavigableMap<Double, List<Employee>> map = new TreeMap<>();
  34. for (Employee e : employees) {
  35. sum += e.getSalary();
  36. map.compute(e.getSalary(), (k, v) -> {
  37. List<Employee> l = (v == null) ? new ArrayList<>() : v;
  38. l.add(e);
  39. return l;
  40. });
  41. }
  42. double avg = sum / employees.size();
  43. System.out.println("avg = " + avg);
  44. System.out.println(">avg = " + map.tailMap(avg, false).values());
  45. }
  46. }
Success #stdin #stdout 0.14s 52916KB
stdin
Standard input is empty
stdout
avg = 140.0
>avg = [[7699a589 150.0, 6acbcfc0 150.0], [5f184fc6 200.0]]