Advertisement
Didart

Dictionary

Dec 5th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Dictionary {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, List<String>> dictionary = new TreeMap<>();
  8.         String[] input = scanner.nextLine().split("\\s+\\|\\s+");
  9.  
  10.         for (String wordAndDef : input) {
  11.  
  12.             String[] definitions = wordAndDef.split("\\s*:\\s*");
  13.             dictionary.putIfAbsent(definitions[0], new ArrayList<>());
  14.  
  15.             for (int i = 1; i < definitions.length; i++) {
  16.                 dictionary.get(definitions[0]).add(definitions[i]);
  17.             }
  18.         }
  19.         String[] print = scanner.nextLine().split("\\s*\\|\\s*");
  20.  
  21.         for (String printWordDef : print) {
  22.             if (dictionary.containsKey(printWordDef)) {
  23.                 System.out.println(printWordDef);
  24.                 dictionary.get(printWordDef).stream().sorted(((e1, e2) ->
  25.                         Integer.compare(e2.length(), e1.length())))
  26.                         .forEach(e -> System.out.println(String.format("-%s", e)));
  27.                 //.forEach(e -> System.out.printf("-%s", e));
  28.             }
  29.         }
  30.         String output = scanner.nextLine();
  31.         if ("List".equals(output)) {
  32.             dictionary.forEach((key, value) -> System.out.print(String.format("%s ", key)));
  33.             //dictionary.forEach((key, value) -> System.out.printf("%s ", key));
  34.         }
  35.     }
  36. }
  37.    
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement