fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.function.*;
  5. import java.util.stream.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Map<String, List<String>> groupsMap = new HashMap<>();
  13. groupsMap.put("1", new ArrayList<>());
  14. groupsMap.get("1").add("Jeff");
  15. groupsMap.get("1").add("Bob");
  16. groupsMap.put("2", new ArrayList<>());
  17. groupsMap.get("2").add("Jeff");
  18.  
  19. String socialButterfly = groupsMap.values()
  20. .stream()
  21. .flatMap(Collection::stream)
  22. .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
  23. .entrySet()
  24. .stream()
  25. .max(Map.Entry.comparingByValue())
  26. .get()
  27. .getKey();
  28. System.out.println(socialButterfly);
  29. }
  30. }
Success #stdin #stdout 0.25s 320960KB
stdin
Standard input is empty
stdout
Jeff