Guest User

Untitled

a guest
Mar 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import java.util.Spliterators;
  2. import java.util.stream.StreamSupport;
  3.  
  4. import static java.util.Comparator.comparingInt;
  5. import static java.util.Spliterator.*;
  6. import static java.util.function.Function.identity;
  7. import static java.util.stream.Collectors.toMap;
  8.  
  9. public class StreamWordCounter {
  10.  
  11. public static void main(String[] args) {
  12. Arrays.stream(args)
  13. .map(String::toLowerCase)
  14. .collect(toMap(identity(), Lexeme::new, Lexeme::merge))
  15. .values()
  16. .stream()
  17. .sorted(comparingInt(Lexeme::getCount).thenComparing(Lexeme::getWord))
  18. .limit(countWords)
  19. .map(Lexeme::getWord)
  20. .forEachOrdered(System.out::println);
  21. }
  22.  
  23. private static class Lexeme {
  24. private final String word;
  25. private final int count;
  26.  
  27. public Lexeme(String word, int count) {
  28. this.word = word;
  29. this.count = count;
  30. }
  31.  
  32. public Lexeme(String word) {
  33. this.word = word;
  34. this.count = 1;
  35. }
  36.  
  37. public String getWord() {
  38. return word;
  39. }
  40.  
  41. public int getCount() {
  42. return count;
  43. }
  44.  
  45. public Lexeme merge(Lexeme other) {
  46. return new Lexeme(word, count + other.count);
  47. }
  48. }
  49. }
Add Comment
Please, Sign In to add comment