fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. List<String> list = new ArrayList<String>();
  14. list.add("a");
  15. list.add("b");
  16. list.add("c");
  17. list.add("d");
  18. list.add("b");
  19. list.add("c");
  20. list.add("a");
  21. list.add("a");
  22. list.add("a");
  23.  
  24. Map<String, Long> counted = list.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
  25.  
  26. System.out.println(counted);
  27. }
  28. }
Success #stdin #stdout 0.13s 4386816KB
stdin
Standard input is empty
stdout
{a=4, b=2, c=2, d=1}