Advertisement
Guest User

Untitled

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