View difference between Paste ID: AZApBz8W and fccVrheX
SHOW: | | - or go back to the newest paste.
1
import java.io.BufferedReader;
2
import java.io.IOException;
3
import java.io.InputStreamReader;
4
import java.util.*;
5
6
public class Linquistics {
7
    public static void main(String[] args) throws IOException {
8
        Map<String, LinkedHashSet<String>> collections = new LinkedHashMap<>();
9
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
10
        while (true) {
11
            String input = reader.readLine();
12
13
            if (input.equals("exit")) {
14
                break;
15
            }
16
17
            String[] tokens = input.split("\\.");
18
            if (tokens.length > 1) {
19
                collections.putIfAbsent(tokens[0], new LinkedHashSet<>());
20
21
                addElements(collections,tokens[0], Arrays.copyOfRange(tokens, 1, tokens.length));
22
            } else if (input.equals("exit")) {
23
                break;
24
            } else if (Character.isDigit(input.charAt(0))) {
25
                int n = Integer.parseInt("" + input.charAt(0));
26
                collections
27
                        .entrySet()
28
                        .stream()
29
                        .sorted((a,b) -> Integer.compare(b.getValue().size(), a.getValue().size()))
30
                        .limit(1)
31
                        .forEach(c -> {
32
                            c
33
                                    .getValue()
34
                                    .stream()
35
                                    .sorted((a,b) -> Integer.compare(a.length(), b.length()))
36
                                    .limit(n)
37
                                    .forEach(m -> {
38
                                        System.out.println("* " + m);
39
                                    });
40
                        });
41
            } else {
42
                if (collections.containsKey(input)) {
43
                    collections.get(input).stream().sorted((a, b) -> {
44
                        int index = Integer.compare(b.length(), a.length());
45
                        if (index == 0) {
46
                            index = countUniqueCharacters(b) - countUniqueCharacters(a);
47
                        }
48
                       return index;
49
                    })
50
                    .forEach(m -> System.out.printf("* %s%n",m));
51
                }
52
            }
53
         }
54
        String[] lastLine = reader.readLine().split("\\s+");
55
56
        String methodToSearch = lastLine[0];
57
        String selectionToSearch = lastLine[1];
58
59
        collections
60
                .entrySet()
61
                .stream()
62
                .filter(a -> a.getValue().contains(methodToSearch))
63
                .sorted((a, b) -> {
64
                    int index = Integer.compare(b.getValue().size(), a.getValue().size());
65
                    if (index == 0) {
66
                        String firstValue = b.getValue().stream().min(Comparator.comparingInt(String::length)).get();
67
                        String secondValue = a.getValue().stream().min(Comparator.comparingInt(String::length)).get();
68
69
                        index = Integer.compare(firstValue.length(), secondValue.length());
70
                    }
71
72
                    return index;
73
                })
74
                .forEach(c -> {
75
                    System.out.println(c.getKey());
76
                    if (selectionToSearch.equals("all")) {
77
                        c.getValue().stream()
78
                                .sorted((m1, m2) -> {
79
                            return Integer.compare(m2.length(), m1.length());
80
                                })
81
                                .forEach(m -> System.out.println("* " + m));
82
                    }
83
                });
84
85
                //for (String key : collections.keySet()) {
86
                //    System.out.println(key + " --> " + collections.get(key));
87
                //}
88
89
    }
90
91
    private static void addElements(Map<String, LinkedHashSet<String>> collections, String key, String[] methods) {
92
        for (String method : methods) {
93
            collections.get(key).add(method.replace("(", "").replace(")", ""));
94
        }
95
    }
96
    public static int countUniqueCharacters(String input)
97
    {
98
        String unique = input.replaceAll("(.)(?=.*?\\1)", "");
99
        return unique.length();
100
    }
101
}