emodev

Dictionary

Dec 8th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package z_finalDemoExam;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Dictionary {
  7.     public static void main(String[] args) {
  8.         Scanner console = new Scanner(System.in);
  9.         String[] line = console.nextLine().split(" \\| ");
  10.  
  11.         HashMap<String, List<String>> dictionary = new HashMap<>();
  12.  
  13.         for (int i = 0; i < line.length; i++) {
  14.             String[] wordAndDefinition = line[i].split(": ");
  15.             String word = wordAndDefinition[0];
  16.             String definition = wordAndDefinition[1];
  17.             if (!dictionary.containsKey(word)) {
  18.                 dictionary.put(word, new ArrayList<>());
  19.             }
  20.             dictionary.get(word).add(definition);
  21.  
  22.         }
  23.  
  24.         String[] keys = console.nextLine().split(" \\| ");
  25.         String command = console.nextLine();
  26.  
  27.         if ("End".equals(command)) {
  28.             HashMap<String, List<String>> sortedfinal = new HashMap<>();
  29.             for (String key : keys) {
  30.                 if (dictionary.containsKey(key)){
  31.                     sortedfinal.put(key,dictionary.get(key));
  32.                 }
  33.             }
  34.             sortedfinal.entrySet().stream().sorted((e1, e2) -> (e1.getKey().compareTo(e2.getKey()))).forEach((e1) -> {
  35.                 System.out.println(e1.getKey());
  36.                 List<String> sorted = e1.getValue().stream().sorted((d1, d2) -> (Integer.compare(d2.length(), d1.length()))).collect(Collectors.toList());
  37.                 for (String s : sorted) {
  38.                     System.out.println(" -" + s);
  39.                 }
  40.  
  41.             });
  42.  
  43.  
  44.         }
  45.  
  46.         if ("List".equals(command)) {
  47.            dictionary.entrySet().stream().sorted((e1, e2) -> (e1.getKey().compareTo(e2.getKey()))).forEach((e) -> System.out.print(e.getKey() + " "));
  48.         }
  49.     }
  50. }
Add Comment
Please, Sign In to add comment