Advertisement
veronikaaa86

02. Word Synonyms

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