Advertisement
Guest User

Anapurana

a guest
Apr 3rd, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.*;
  3.  
  4. public class OntheWaytoAnnapurna {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         Map<String, List<String>> map = new HashMap<>();
  8.         String input = scan.nextLine();
  9.         while (!input.equals("END")) {
  10.             String[] tokens = input.split("->");
  11.             String command = tokens[0];
  12.             String store = tokens[1];
  13.  
  14.             switch (command) {
  15.                 case "Add":
  16.                     List<String> list = new ArrayList<>(Arrays.asList(tokens[2].split(",")));
  17.  
  18.                     if (list.size() > 1){
  19.                         map.putIfAbsent(store, new ArrayList<>());
  20.                         for (String s : list) {
  21.                             map.get(store).add(s);
  22.                         }
  23.                     } else {
  24.                         String item = tokens[2];
  25.                         map.putIfAbsent(store, new ArrayList<>());
  26.                         map.get(store).add(item);
  27.                     }
  28.                     break;
  29.                 case "Remove":
  30.                     if (map.containsKey(store)){
  31.                         map.remove(store);
  32.                     }
  33.                     break;
  34.             }
  35.             input = scan.nextLine();
  36.         }
  37.         System.out.println("Stores list:");
  38.         map
  39.                 .entrySet()
  40.                 .stream()
  41.                 .sorted((a,b) -> b.getKey().compareTo(a.getKey()))//Help
  42.                 .sorted((a, b) -> b.getValue().size() - a.getValue().size())
  43.                 .forEach(entry -> {
  44.                     System.out.println(entry.getKey());
  45.                     entry.getValue().forEach(item -> System.out.println("<<" + item + ">>"));
  46.                 });
  47.  
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement