fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. import static java.util.stream.Collectors.counting;
  6. import static java.util.stream.Collectors.groupingBy;
  7.  
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<String> list = Arrays.asList("a", "a", "d", "c", "d", "c", "a", "d");
  13.  
  14. Map<String, Long> count = list.stream()
  15. .collect(
  16. groupingBy(
  17. s -> s,
  18. () -> new HashMap<>(), // Map<String, Long>
  19. counting()
  20. )
  21. );
  22. System.out.println(count);
  23.  
  24. count = list.stream()
  25. .collect(
  26. groupingBy(
  27. s -> s,
  28. () -> new HashMap<String, Long>(),
  29. counting()
  30. )
  31. );
  32. System.out.println(count);
  33.  
  34. /*count = list.stream()
  35.   .collect(
  36.   groupingBy(
  37.   s -> s,
  38.   () -> new HashMap<String, long[]>(),
  39.   counting()
  40.   )
  41.   );
  42.   System.out.println(count);*/
  43. }
  44. }
Success #stdin #stdout 0.16s 2184192KB
stdin
Standard input is empty
stdout
{a=3, c=2, d=3}
{a=3, c=2, d=3}