Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Dictionary {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, List<String>> dict = new HashMap<>();
- String[] line = scanner.nextLine().split("\\|");
- for (int i = 0; i < line.length; i++) {
- String[] tokens = line[i].split(": ");
- String word = tokens[0].trim();
- String definition = tokens[1].trim();
- dict.putIfAbsent(word, new ArrayList<>());
- dict.get(word).add(definition);
- }
- String[] newLine = scanner.nextLine().split("\\|");
- Map<String, List<String>> finalMap = new HashMap<>();
- for (String s : dict.keySet()) {
- for (int i = 0; i < newLine.length; i++) {
- if (s.equals(newLine[i])) {
- finalMap.putIfAbsent(s, new ArrayList<>());
- finalMap.put(s, dict.get(s));
- }
- }
- }
- String command = scanner.nextLine();
- if (command.equals("List")) {
- dict.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
- .forEach(e -> {
- System.out.print(String.format("%s ", e.getKey()));
- });
- } else {
- finalMap.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
- .forEach(e -> {
- System.out.println(e.getKey());
- e.getValue().stream().sorted((es1, es2) -> {
- return es2.length() - es1.length();
- }).forEach(x -> {
- System.out.println(String.format(" -%s", x));
- });
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment