Advertisement
mirozspace

Untitled

Apr 6th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 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.         Map<String, List<String>> wordsAndDefinitions = new TreeMap<>();
  7.  
  8.         String[] line = scanner.nextLine().split(" \\| ");
  9.         for (int i = 0; i < line.length; i++) {
  10.             String[] wordsAndDefinition = line[i].split(": ");
  11.             String word = wordsAndDefinition[0];
  12.             String definition = wordsAndDefinition[1];
  13.  
  14.             wordsAndDefinitions.putIfAbsent(word, new ArrayList<>());
  15.             List<String> temp = wordsAndDefinitions.get(word);
  16.             if (!temp.contains(definition)) {
  17.                 temp.add(definition);
  18.             }
  19.         }
  20.         String[] searchedWords = scanner.nextLine().split(" \\| ");
  21.         for (int i = 0; i < searchedWords.length; i++) {
  22.             if (wordsAndDefinitions.containsKey(searchedWords[i])) {
  23.                 System.out.println(searchedWords[i]);
  24.                 List<String> temp = wordsAndDefinitions.get(searchedWords[i]);
  25.                 Collections.sort(temp, Comparator.comparing(String::length));
  26.                 Collections.reverse(temp);
  27.                 for (int j = 0; j < temp.size(); j++) {
  28.                     System.out.println(" -" + temp.get(j));
  29.                 }
  30.             }
  31.         }
  32.         String command = scanner.nextLine();
  33.         if (command.equals("List")) {
  34.             wordsAndDefinitions
  35.                     .entrySet()
  36.                     .stream()
  37.                     .sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey()))
  38.                     .forEach(e -> System.out.print(String.format("%s ", e.getKey())));
  39.         } else if (command.equals("End")) {
  40.             return;
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement