View difference between Paste ID: ugC7A599 and nC9QPLJM
SHOW: | | - or go back to the newest paste.
1
import javafx.util.Pair;
2
import java.io.BufferedReader;
3
import java.io.IOException;
4-
import java.util.Scanner;
4+
import java.io.InputStreamReader;
5
import java.util.HashMap;
6
import java.util.LinkedHashMap;
7-
    public static void main(String[] args) {
7+
8-
        Scanner scanner = new Scanner(System.in);
8+
9
    public static void main(String[] args) throws IOException {
10
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
11
12
        LinkedHashMap<Pair<String, String>, Integer> dwarfs = new LinkedHashMap<>();
13-
        while (true) {
13+
14-
            String line = scanner.nextLine();
14+
15
        String line;
16-
            if (line.equals("Once upon a time"))
16+
        while (!"Once upon a time".equals(line=reader.readLine())) {
17-
                break;
17+
18
            String dwarfName = tokens[0];
19
            String dwarfHatColor = tokens[1];
20
            Integer dwarfPhysics = Integer.parseInt(tokens[2]);
21
22
            Pair<String, String> dwarf = new Pair<>(dwarfName, dwarfHatColor);
23
24
            if (dwarfs.containsKey(dwarf)) {
25
                Integer oldPhysics = dwarfs.get(dwarf);
26
                if (dwarfPhysics > oldPhysics) {
27
                    dwarfs.put(dwarf, dwarfPhysics);
28
                }
29
            } else {
30
                dwarfs.put(dwarf, dwarfPhysics);
31
                colors.putIfAbsent(dwarfHatColor, 0);
32
                colors.put(dwarfHatColor, colors.get(dwarfHatColor) + 1);
33
            }
34
        }
35
36
        dwarfs.entrySet()
37
                .stream()
38
                .sorted((d1, d2) -> {
39
                    int result = Integer.compare(d2.getValue(), d1.getValue());
40
41
                    if (result == 0) {
42
                        result = Integer.compare(colors.get(d2.getKey().getValue()),
43
                                colors.get(d1.getKey().getValue()));
44
                    }
45
46
                    return result;
47
                })
48
                .forEach(d -> System.out.printf("(%s) %s <-> %d%n",
49
                        d.getKey().getValue(),
50
                        d.getKey().getKey(),
51
                        d.getValue()));
52
    }
53
}