fork download
  1. import java.util.*;
  2.  
  3. class Main {
  4. public static void main(String[] args) {
  5. final Map<String, Integer> map = new HashMap<>();
  6. map.put("first", 50);
  7. map.put("second", 10);
  8. map.put("third", 50);
  9.  
  10. List<String> keys = map.entrySet().stream()
  11. .collect(() -> new Object() {
  12. int max = Integer.MIN_VALUE;
  13. List<String> keys = new ArrayList<>();
  14. }, (current, next) -> {
  15. int value = next.getValue();
  16. if(value >= current.max) {
  17. if(value > current.max) {
  18. current.max = value;
  19. current.keys.clear();
  20. }
  21. current.keys.add(next.getKey());
  22. }
  23. }, (a, b) -> {
  24. if(a.max <= b.max) {
  25. a.max = b.max;
  26. a.keys = b.keys;
  27. }
  28. else if(a.max == b.max) a.keys.addAll(b.keys);
  29. }).keys;
  30.  
  31.  
  32. System.out.println(keys);
  33. }
  34. }
Success #stdin #stdout 0.09s 50556KB
stdin
Standard input is empty
stdout
[third, first]