Advertisement
ilianrusev

(DEMO)Final Exam - 06 April 2019 - 01. Dictionary

Apr 13th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.TreeMap;
  8.  
  9. public class Main {
  10.     public static void main(String[] args) {
  11.         @SuppressWarnings("resource")
  12.  
  13.         Scanner sc = new Scanner(System.in);
  14.  
  15.         String[] input = sc.nextLine().split("\\s+\\|\\s+");
  16.  
  17.         Map<String, List<String>> words = new TreeMap<>();
  18.  
  19.         for (int i = 0; i < input.length; i++) {
  20.             String[] tokens = input[i].split(":\\s+");
  21.             String word = tokens[0];
  22.             String def = tokens[1];
  23.  
  24.             if (!words.containsKey(word)) {
  25.                 words.put(word, new ArrayList<>());
  26.                 words.get(word).add(def);
  27.             } else {
  28.                 words.get(word).add(def);
  29.             }
  30.         }
  31.  
  32.         String[] wordsInDic = sc.nextLine().split("\\s+\\|\\s+");
  33.  
  34.         for (int i = 0; i < wordsInDic.length; i++) {
  35.             if (words.containsKey(wordsInDic[i])) {
  36.                 System.out.println(wordsInDic[i]);
  37.                 words.get(wordsInDic[i]).stream().sorted((d1, d2) -> {
  38.                     int sort = Integer.compare(d2.length(), d1.length());
  39.                     return sort;
  40.                 }).forEach(e -> System.out.println(String.format(" -%s", e.toString().replaceAll("\\[|\\]", ""))));
  41.             }
  42.         }
  43.  
  44.         String option = sc.nextLine();
  45.  
  46.         switch (option) {
  47.             case "End":
  48.                 break;
  49.             case "List":
  50.                 List<String> keys = new ArrayList<>();
  51.                 words.keySet().stream().forEach(e -> keys.add(e));
  52.                 System.out.println(keys.toString().replaceAll("\\[|\\]|,", ""));
  53.                 break;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement