Advertisement
LoraOrliGeo

P1_Dictionary_DemoFinalExam

Apr 12th, 2019
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package a3_Demo_FinalExem_6Apr2019;
  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 P1_Dictionary {
  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.         String option = sc.nextLine();
  35.  
  36.         switch (option) {
  37.         case "End":
  38.             for (int i = 0; i < wordsInDic.length; i++) {
  39.                 if (words.containsKey(wordsInDic[i])) {
  40.                     System.out.println(wordsInDic[i]);
  41.                     words.get(wordsInDic[i]).stream().sorted((d1, d2) -> {
  42.                         int sort = Integer.compare(d2.length(), d1.length());
  43.                         return sort;
  44.                     }).forEach(e -> System.out.println(String.format(" -%s", e.toString().replaceAll("\\[|\\]", ""))));
  45.                 }
  46.             }
  47.             break;
  48.         case "List":
  49.             List<String> keys = new ArrayList<>();
  50.             words.keySet().stream().forEach(e -> keys.add(e));
  51.             System.out.println(keys.toString().replaceAll("\\[|\\]|,", ""));
  52.             break;
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement