Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class SantasNewList {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- TreeMap<String, Integer> santaList = new TreeMap<>();
- LinkedHashMap<String, Integer> santaList2 = new LinkedHashMap<>();
- String input = scanner.nextLine();
- while (!input.equals("END")) {
- String[] tokens = input.split("->");
- String name = tokens[0];
- if (name.equals("Remove")) {
- santaList.remove(tokens[1]);
- } else {
- name = tokens[0];
- String typeGift = tokens[1];
- Integer amount = Integer.parseInt(tokens[2]);
- if (!santaList.containsKey(name)){
- santaList.put(name,amount);
- if (!santaList2.containsKey(typeGift)){
- santaList2.put(typeGift,amount);
- }else{
- int current = santaList2.get(typeGift);
- santaList2.put(typeGift,current + amount);
- }
- }else{
- int current = santaList.get(name);
- santaList.put(name,current + amount);
- }
- }
- input = scanner.nextLine();
- }
- System.out.println("Children:");
- santaList.entrySet().stream().sorted((f,s)->
- Integer.compare(s.getValue(),f.getValue())
- ) .forEach(e -> {
- System.out.println(String.format("%s -> %s",e.getKey(), e.getValue()));
- });
- System.out.println("Presents:");
- santaList2.entrySet().forEach(e -> {
- System.out.println(String.format("%s -> %s",e.getKey(), e.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement