Advertisement
Guest User

Snowwhite

a guest
Jan 10th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import javafx.util.Pair;
  2. import java.util.HashMap;
  3. import java.util.LinkedHashMap;
  4. import java.util.Scanner;
  5.  
  6. public class Snowwhite {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         LinkedHashMap<Pair<String, String>, Integer> dwarfs = new LinkedHashMap<>();
  11.         HashMap<String, Integer> colors = new HashMap<>();
  12.  
  13.         while (true) {
  14.             String line = scanner.nextLine();
  15.  
  16.             if (line.equals("Once upon a time"))
  17.                 break;
  18.  
  19.             String[] tokens = line.split(" <:> ");
  20.             String dwarfName = tokens[0];
  21.             String dwarfHatColor = tokens[1];
  22.             Integer dwarfPhysics = Integer.parseInt(tokens[2]);
  23.  
  24.             Pair<String, String> dwarf = new Pair<>(dwarfName, dwarfHatColor);
  25.  
  26.             if (dwarfs.containsKey(dwarf)) {
  27.                 Integer oldPhysics = dwarfs.get(dwarf);
  28.                 if (dwarfPhysics > oldPhysics) {
  29.                     dwarfs.put(dwarf, dwarfPhysics);
  30.                 }
  31.             } else {
  32.                 dwarfs.put(dwarf, dwarfPhysics);
  33.                 colors.putIfAbsent(dwarfHatColor, 0);
  34.                 colors.put(dwarfHatColor, colors.get(dwarfHatColor) + 1);
  35.             }
  36.         }
  37.  
  38.         dwarfs.entrySet()
  39.                 .stream()
  40.                 .sorted((d1, d2) -> {
  41.                     int result = Integer.compare(d2.getValue(), d1.getValue());
  42.  
  43.                     if (result == 0) {
  44.                         result = Integer.compare(colors.get(d2.getKey().getValue()),
  45.                                 colors.get(d1.getKey().getValue()));
  46.                     }
  47.  
  48.                     return result;
  49.                 })
  50.                 .forEach(d -> System.out.printf("(%s) %s <-> %d%n",
  51.                         d.getKey().getValue(),
  52.                         d.getKey().getKey(),
  53.                         d.getValue()));
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement