marking2112

Dictionary

Dec 10th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 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.  
  7.  
  8.         Map<String, List<String>> dict = new HashMap<>();
  9.  
  10.         String[] line = scanner.nextLine().split("\\|");
  11.  
  12.         for (int i = 0; i < line.length; i++) {
  13.             String[] tokens = line[i].split(": ");
  14.             String word = tokens[0].trim();
  15.             String definition = tokens[1].trim();
  16.  
  17.             dict.putIfAbsent(word, new ArrayList<>());
  18.             dict.get(word).add(definition);
  19.  
  20.         }
  21.  
  22.         String[] newLine = scanner.nextLine().split("\\|");
  23.         Map<String, List<String>> finalMap = new HashMap<>();
  24.  
  25.         for (String s : dict.keySet()) {
  26.             for (int i = 0; i < newLine.length; i++) {
  27.                 if (s.equals(newLine[i])) {
  28.                     finalMap.putIfAbsent(s, new ArrayList<>());
  29.                     finalMap.put(s, dict.get(s));
  30.                 }
  31.             }
  32.         }
  33.  
  34.         String command = scanner.nextLine();
  35.  
  36.         if (command.equals("List")) {
  37.             dict.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
  38.                     .forEach(e -> {
  39.                         System.out.print(String.format("%s ", e.getKey()));
  40.                     });
  41.         } else {
  42.             finalMap.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
  43.                     .forEach(e -> {
  44.                         System.out.println(e.getKey());
  45.                         e.getValue().stream().sorted((es1, es2) -> {
  46.                             return es2.length() - es1.length();
  47.  
  48.                         }).forEach(x -> {
  49.                             System.out.println(String.format(" -%s", x));
  50.                         });
  51.                     });
  52.         }
  53.  
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment