Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class p01_countOfWords {
  6. public static void main(String[] args) {
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. int numberOfLines = Integer.parseInt(scan.nextLine());
  10. String text;
  11. String[] words;
  12.  
  13. Map<String, Integer> wordsAndTheirCount = new LinkedHashMap<>();
  14.  
  15. for (int i = 0; i < numberOfLines; i++) {
  16. text = scan.nextLine();
  17.  
  18. words = text.split("\\s+");
  19.  
  20. for (int j = 0; j < words.length; j++) {
  21. String currentWord = words[j].toLowerCase();
  22.  
  23. if (!wordsAndTheirCount.containsKey(currentWord)){
  24. wordsAndTheirCount.put(currentWord, 1);
  25. } else {
  26. wordsAndTheirCount.put(currentWord, wordsAndTheirCount.get(currentWord) + 1);
  27. }
  28. }
  29. }
  30.  
  31. wordsAndTheirCount.entrySet().stream()
  32. .sorted((kv1, kv2) -> Integer.compare(kv2.getValue(), kv1.getValue()))
  33. .forEach(pair -> System.out.printf("Word: %s --> Count: %d%n", pair.getKey(), pair.getValue()));
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement