Advertisement
veronikaaa86

02. Word Synonyms

Nov 3rd, 2021
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. package mapAndStreamAPI;
  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.  
  13. for (int i = 0; i < n; i++) {
  14. String word = scanner.nextLine();
  15. String synonym = scanner.nextLine();
  16.  
  17. if (!wordsMap.containsKey(word)) {
  18. wordsMap.put(word, new ArrayList<>());
  19. wordsMap.get(word).add(synonym);
  20. } else {
  21. wordsMap.get(word).add(synonym);
  22. }
  23. }
  24.  
  25. //cute - adorable, charming
  26. for (Map.Entry<String, List<String>> entry : wordsMap.entrySet()) {
  27. System.out.printf("%s - %s%n",
  28. entry.getKey(), String.join(", ", entry.getValue()));
  29. }
  30.  
  31. }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement