Advertisement
veronikaaa86

02. Word Synonyms

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