Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class OnTheWayToAnnapurna02 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- LinkedHashMap<String, List<String>> storesAndItems = new LinkedHashMap<>();
- String input = sc.nextLine();
- while (!input.equals("END")) {
- String[] tokens = input.split("->");
- String store = tokens[1];
- String commands = tokens[0];
- switch (commands) {
- case "Add": {
- String item = tokens[2];
- List<String> items = Arrays.stream(tokens[2].split(","))
- .collect(Collectors.toList());
- if (!storesAndItems.containsKey(store)) {
- storesAndItems.put(store, items);
- } else {
- storesAndItems.get(store).add(item);
- }
- }
- break;
- case "Remove":
- if (storesAndItems.size() < 2)
- break;
- String toRemove = tokens[1];
- storesAndItems.remove(toRemove);
- break;
- }
- input = sc.nextLine();
- }
- System.out.println("Stores list:");
- storesAndItems
- .entrySet()
- .stream()
- .sorted((a, b) -> {
- int result = b.getValue().size() - a.getValue().size();
- if (result == 0) {
- result = b.getKey().compareTo(a.getKey());
- }
- return result;
- })
- .forEach((e) -> {
- System.out.println(e.getKey());
- e.getValue()
- .forEach(value -> System.out.println(String.format("<<%s>>", value)));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement