Didart

Word Synonyms

Nov 3rd, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class WordSynonyms {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.  
  9.         Map<String, List<String>> wordsMap = new LinkedHashMap<>();
  10.  
  11.         for (int i = 0; i < n; i++) {
  12.             String word = scanner.nextLine();
  13.             String synonym = scanner.nextLine();
  14.  
  15.             if (!wordsMap.containsKey(word)) {
  16.                 wordsMap.put(word, new ArrayList<>());
  17.                 wordsMap.get(word).add(synonym);
  18.             } else {
  19.                 wordsMap.get(word).add(synonym);
  20.             }
  21.         }
  22.         for (Map.Entry<String, List<String>> entry : wordsMap.entrySet()) {
  23.             System.out.printf("%s - %s%n",
  24.                     entry.getKey(), String.join(", ", entry.getValue()));
  25.         }
  26.     }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment