Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package z_finalDemoExam;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Dictionary {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- String[] line = console.nextLine().split(" \\| ");
- HashMap<String, List<String>> dictionary = new HashMap<>();
- for (int i = 0; i < line.length; i++) {
- String[] wordAndDefinition = line[i].split(": ");
- String word = wordAndDefinition[0];
- String definition = wordAndDefinition[1];
- if (!dictionary.containsKey(word)) {
- dictionary.put(word, new ArrayList<>());
- }
- dictionary.get(word).add(definition);
- }
- String[] keys = console.nextLine().split(" \\| ");
- String command = console.nextLine();
- if ("End".equals(command)) {
- HashMap<String, List<String>> sortedfinal = new HashMap<>();
- for (String key : keys) {
- if (dictionary.containsKey(key)){
- sortedfinal.put(key,dictionary.get(key));
- }
- }
- sortedfinal.entrySet().stream().sorted((e1, e2) -> (e1.getKey().compareTo(e2.getKey()))).forEach((e1) -> {
- System.out.println(e1.getKey());
- List<String> sorted = e1.getValue().stream().sorted((d1, d2) -> (Integer.compare(d2.length(), d1.length()))).collect(Collectors.toList());
- for (String s : sorted) {
- System.out.println(" -" + s);
- }
- });
- }
- if ("List".equals(command)) {
- dictionary.entrySet().stream().sorted((e1, e2) -> (e1.getKey().compareTo(e2.getKey()))).forEach((e) -> System.out.print(e.getKey() + " "));
- }
- }
- }
Add Comment
Please, Sign In to add comment