Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import com.sun.source.tree.WhileLoopTree;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, List<String>> data = new LinkedHashMap<>();
- String input = "";
- System.out.println();
- while (!"END".equals(input = scanner.nextLine())){
- String[] splitted = input.split("\\b[-][>]+\\b");
- if (splitted[0].equals("Add")){
- List<String> items = new ArrayList<>();
- String[] splitedItems = splitted[2].split("[,]+");
- Collections.addAll(items,splitedItems);
- data.putIfAbsent(splitted[1],new ArrayList<>());
- for (int i = 0; i < items.size(); i++) {
- data.get(splitted[1]).add(items.get(i));
- }
- }else if (splitted[0].equals("Remove")){
- if (data.containsKey(splitted[1])){
- data.remove(splitted[1]);
- }
- }
- }
- System.out.println("Stores list:");
- data.entrySet()
- .stream()
- .sorted((a,b) -> {
- int sort = Integer.compare(b.getValue().size(), a.getValue().size());
- if (sort == 0){
- sort = b.getKey().compareTo(a.getKey());
- }
- return sort;
- }).forEach(e -> {
- System.out.println(String.format("%s",e.getKey()));
- data.get(e.getKey()).forEach(k ->{
- System.out.printf("<<%s>>\n",k);
- });
- });
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement