Commit | Line | Data |
---|---|---|
437984e3 JB |
1 | import java.io.File; |
2 | import java.util.Scanner; | |
3 | import java.io.IOException; | |
a1aa04de | 4 | import java.util.Map; |
437984e3 | 5 | import java.util.TreeMap; |
fb7a0378 | 6 | import java.util.SortedMap; |
a1aa04de | 7 | import java.util.stream.Collectors; |
2260b7d6 JB |
8 | |
9 | class Main { | |
10 | ||
11 | /** | |
12 | * The main() function | |
13 | * @param String[] args main() function arguments array | |
14 | */ | |
15 | public static void main(String[] args) { | |
80eafbcb | 16 | // default TreeMap comparator keep alphabetical order |
a1aa04de JB |
17 | // NOTE: words comparaison is case insensitive and punctuation aware |
18 | TreeMap<String, Integer> tm = new TreeMap<String, Integer>(new StrComparator()); | |
19 | // NOTE: words comparaison is case sensitive and punctuation aware | |
20 | //TreeMap<String, Integer> tm = new TreeMap<String, Integer>(); | |
437984e3 JB |
21 | File f = new File("test_file.txt"); |
22 | try { | |
23 | Scanner sc = new Scanner(f); | |
24 | while (sc.hasNext()) { | |
25 | String wordCursor = sc.next(); | |
437984e3 JB |
26 | Integer count = tm.get(wordCursor); |
27 | if (count == null) { | |
28 | count = 0; | |
29 | } | |
30 | tm.put(wordCursor, count + 1); | |
31 | } | |
32 | sc.close(); | |
33 | } | |
34 | catch (IOException e) { | |
35 | e.printStackTrace(); | |
36 | } | |
fb7a0378 JB |
37 | |
38 | System.out.println("TreeMap full content:"); | |
437984e3 JB |
39 | for (String wordCursor : tm.keySet()) { |
40 | System.out.println("Word \"" + wordCursor + "\" occured " + tm.get(wordCursor) + " times"); | |
41 | } | |
fb7a0378 | 42 | |
a1aa04de JB |
43 | System.out.println("SortedMap content of words inferior to the word \"milieu\":"); |
44 | SortedMap<String, Integer> sm = tm.headMap("milieu"); | |
143582d5 JB |
45 | for (String wordCursor : sm.keySet()) { |
46 | System.out.println("Word \"" + wordCursor + "\" occured " + sm.get(wordCursor) + " times"); | |
fb7a0378 | 47 | } |
a1aa04de | 48 | |
3ba1ba1a | 49 | System.out.println("Map content of exactly duplicated word inferior to the word \"milieu\":"); |
a1aa04de JB |
50 | Map<String, Integer> m1 = tm.entrySet().stream() |
51 | .filter(map -> map.getKey().compareTo("milieu") < 0 && map.getValue() == 2) | |
52 | .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); | |
53 | for (String wordCursor : m1.keySet()) { | |
0da45dfa | 54 | System.out.println("Word \"" + wordCursor + "\" occured " + m1.get(wordCursor) + " times"); |
a1aa04de JB |
55 | } |
56 | ||
57 | System.out.println("Map content of duplicates with occurence between 2 and 5:"); | |
58 | Map<String, Integer> m2 = tm.entrySet().stream() | |
59 | .filter(map -> map.getValue() >= 2 && map.getValue() <= 5) | |
60 | .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); | |
61 | for (String wordCursor : m2.keySet()) { | |
0da45dfa | 62 | System.out.println("Word \"" + wordCursor + "\" occured " + m2.get(wordCursor) + " times"); |
a1aa04de | 63 | } |
2260b7d6 JB |
64 | } |
65 | } |