fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. import java.util.function.*;
  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. String input = "tacocat backwards is tacocat";
  13. String[] words = input.split( " " );
  14. Map < String, Long > wordCount =
  15. .stream( words )
  16. .collect(
  17. Collectors.groupingBy(
  18. Function.identity() ,
  19. Collectors.counting()
  20. )
  21. );
  22. System.out.println( wordCount );
  23.  
  24. Map < String, Long > dupCount =
  25. wordCount
  26. .entrySet()
  27. .stream()
  28. .filter(
  29. entry -> entry.getValue() > 1
  30. )
  31. .collect(
  32. Collectors.toMap(
  33. entry -> entry.getKey() ,
  34. entry -> entry.getValue()
  35. )
  36. );
  37. System.out.println( dupCount );
  38. }
  39. }
Success #stdin #stdout 0.11s 50304KB
stdin
Standard input is empty
stdout
{tacocat=2, is=1, backwards=1}
{tacocat=2}