fork(1) 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. import java.util.function.*;
  8.  
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. Stream<String> ss = Arrays.stream(new String[] {"a,b,c","a,c,d","d,b,c"});
  16.  
  17. Map<String,Integer> fm = ss
  18. .map(line -> line.split(","))
  19. .flatMap(Arrays::stream)
  20. .map(String::trim)
  21. .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> 1)));
  22. for (Map.Entry<String,Integer> e : fm.entrySet()) {
  23. System.out.println(e.getKey()+" "+e.getValue());
  24. }
  25. }
  26. }
Success #stdin #stdout 0.21s 2841600KB
stdin
a,b,c,d,
e,a,b,d,
b,b,c,d,a
stdout
a 2
b 2
c 3
d 2