Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class p01_countOfWords {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int numberOfLines = Integer.parseInt(scan.nextLine());
- String text;
- String[] words;
- Map<String, Integer> wordsAndTheirCount = new LinkedHashMap<>();
- for (int i = 0; i < numberOfLines; i++) {
- text = scan.nextLine();
- words = text.split("\\s+");
- for (int j = 0; j < words.length; j++) {
- String currentWord = words[j].toLowerCase();
- if (!wordsAndTheirCount.containsKey(currentWord)){
- wordsAndTheirCount.put(currentWord, 1);
- } else {
- wordsAndTheirCount.put(currentWord, wordsAndTheirCount.get(currentWord) + 1);
- }
- }
- }
- wordsAndTheirCount.entrySet().stream()
- .sorted((kv1, kv2) -> Integer.compare(kv2.getValue(), kv1.getValue()))
- .forEach(pair -> System.out.printf("Word: %s --> Count: %d%n", pair.getKey(), pair.getValue()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement