Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- public class P2 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Map<String, List<String>> notes = new LinkedHashMap<>();
- String line = "";
- while (!"END".equals(line = sc.nextLine())) {
- String[] tokens = line.split("->");
- String command = tokens[0];
- String store = tokens[1];
- switch (command) {
- case "Add":
- if (!tokens[2].contains(",")) {
- String item = tokens[2];
- if (!notes.containsKey(store)) {
- notes.put(store, new ArrayList<>());
- }
- notes.get(store).add(item);
- } else {
- String[] items = tokens[2].split(",");
- for (int i = 0; i < items.length; i++) {
- if (!notes.containsKey(store)) {
- notes.put(store, new ArrayList<>());
- }
- notes.get(store).add(items[i]);
- }
- }
- break;
- case "Remove":
- if (notes.containsKey(store)) {
- notes.remove(store);
- }
- break;
- }
- }
- System.out.println("Stores list:");
- notes.entrySet().stream().sorted((m1, m2) -> {
- int sort = Integer.compare(m2.getValue().size(), m1.getValue().size());
- if (sort == 0) {
- sort = m2.getKey().compareTo(m1.getKey());
- }
- return sort;
- }).forEach(e -> {
- System.out.println(e.getKey());
- for (String item : e.getValue()) {
- System.out.println(String.format("<<%s>>", item));
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement