Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Fundamentals.Final_Exam._14_04_18_Second_Group;
- import JavaBasics.upr17exarciseExams.MaidenParty;
- import java.util.*;
- import java.util.stream.Collectors;
- public class On_The_Way_to_Annapurna {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Map<String , List<String>> map = new LinkedHashMap<>();
- String input = sc.nextLine();
- while (!input.equals("END")){
- String[] array = input.split("->");
- if(array[0].equals("Add")){
- String store = array[1];
- String[] items = array[2].split(",");
- map.putIfAbsent(store, new ArrayList<>());
- for (int i = 0; i < items.length; i++) {
- map.get(store).add(items[i]);
- }
- } else{
- String store = array[1];
- if(map.containsKey(store)){
- map.remove(store);
- }
- }
- input = sc.nextLine();
- }
- Map<String, Integer> mapCount = new LinkedHashMap<>();
- for (Map.Entry<String, List<String>> entry : map.entrySet()) {
- mapCount.put(entry.getKey(),entry.getValue().size());
- }
- mapCount = mapCount.entrySet().stream() // sorting
- .sorted(Map.Entry.<String,Integer>comparingByValue().reversed().thenComparing(Map.Entry.<String,Integer>comparingByKey().reversed()))
- .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(e1, e2)->e1,LinkedHashMap::new));
- System.out.println("Stores list:");
- for (Map.Entry<String, Integer> entry : mapCount.entrySet()) {
- System.out.println(entry.getKey());
- map.get(entry.getKey()).forEach(e-> System.out.println("<<"+e+">>"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment