|
|
@@ -20,33 +20,34 @@ public class WC {
|
|
20
|
20
|
}
|
|
21
|
21
|
}
|
|
22
|
22
|
|
|
23
|
|
- public WC(Iterator<String> si) {
|
|
24
|
|
- this.si = si;
|
|
25
|
|
-
|
|
26
|
|
- }
|
|
27
|
|
-
|
|
28
|
|
- public Iterator<String> getSi() {
|
|
29
|
|
-
|
|
30
|
|
- return si;
|
|
31
|
|
- }
|
|
32
|
|
-
|
|
33
|
|
-
|
|
34
|
|
- private HashMap<String, Integer> readFile() {
|
|
|
23
|
+// public WC(Iterator<String> si) {
|
|
|
24
|
+// this.si = si;
|
|
|
25
|
+//
|
|
|
26
|
+// }
|
|
|
27
|
+//
|
|
|
28
|
+// public Iterator<String> getSi() {
|
|
|
29
|
+//
|
|
|
30
|
+// return si;
|
|
|
31
|
+// }
|
|
|
32
|
+
|
|
|
33
|
+
|
|
|
34
|
+ public HashMap<String, Integer> readFile() {
|
|
35
|
35
|
HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
|
|
36
|
36
|
|
|
37
|
|
- while(getSi().hasNext()) {
|
|
38
|
|
- if(wordCount.containsKey(si.next())) {
|
|
39
|
|
- int value = wordCount.get(si.next());
|
|
|
37
|
+ while(si.hasNext()) {
|
|
|
38
|
+ String key = si.next().toLowerCase();
|
|
|
39
|
+ if(wordCount.containsKey(key)) {
|
|
|
40
|
+ int value = wordCount.get(key);
|
|
40
|
41
|
value++;
|
|
41
|
|
- wordCount.put(si.next(), value);
|
|
|
42
|
+ wordCount.put(key, value);
|
|
42
|
43
|
} else {
|
|
43
|
|
- wordCount.put(si.next(), 1);
|
|
|
44
|
+ wordCount.put(key, 1);
|
|
44
|
45
|
}
|
|
45
|
46
|
}
|
|
46
|
47
|
return wordCount;
|
|
47
|
48
|
}
|
|
48
|
49
|
|
|
49
|
|
- public void printWordCount(HashMap<String, Integer> myMap) {
|
|
|
50
|
+ public static void printWordCount(HashMap<String, Integer> myMap) {
|
|
50
|
51
|
for (String word: myMap.keySet()){
|
|
51
|
52
|
|
|
52
|
53
|
String key = word;
|
|
|
@@ -57,11 +58,49 @@ public class WC {
|
|
57
|
58
|
}
|
|
58
|
59
|
}
|
|
59
|
60
|
|
|
|
61
|
+ public static LinkedHashMap<String, Integer> sortList(HashMap<String, Integer> myMap) {
|
|
|
62
|
+ List<String> mapKeys = new ArrayList<String>(myMap.keySet());
|
|
|
63
|
+ List<Integer> mapValues = new ArrayList<Integer>(myMap.values());
|
|
|
64
|
+ Collections.sort(mapValues);
|
|
|
65
|
+ Collections.sort(mapKeys);
|
|
|
66
|
+
|
|
|
67
|
+ LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
|
|
|
68
|
+ Iterator<Integer> valueIt = mapValues.iterator();
|
|
|
69
|
+ while (valueIt.hasNext()) {
|
|
|
70
|
+ Integer val = valueIt.next();
|
|
|
71
|
+ Iterator<String> keyIt = mapKeys.iterator();
|
|
|
72
|
+
|
|
|
73
|
+ while (keyIt.hasNext()) {
|
|
|
74
|
+ String key = keyIt.next();
|
|
|
75
|
+ Integer comp1 = myMap.get(key);
|
|
|
76
|
+ Integer comp2 = val;
|
|
|
77
|
+
|
|
|
78
|
+ if (comp1.equals(comp2)) {
|
|
|
79
|
+ keyIt.remove();
|
|
|
80
|
+ sortedMap.put(key, val);
|
|
|
81
|
+ break;
|
|
|
82
|
+ }
|
|
|
83
|
+ }
|
|
|
84
|
+ }
|
|
|
85
|
+ return sortedMap;
|
|
|
86
|
+
|
|
|
87
|
+
|
|
|
88
|
+// Map<String, Integer> sorted = myMap
|
|
|
89
|
+// .entrySet()
|
|
|
90
|
+// .stream()
|
|
|
91
|
+// .sorted(comparingByValue())
|
|
|
92
|
+// .collect(
|
|
|
93
|
+// toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2,
|
|
|
94
|
+// LinkedHashMap::new));
|
|
|
95
|
+
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
60
|
98
|
public static void main(String[] args) {
|
|
61
|
|
- String fileName = "./../../someTextFile.txt";
|
|
|
99
|
+ String fileName = "./../../LittleWomen.txt";
|
|
62
|
100
|
String fullPath = WC.class.getResource(fileName).getFile();
|
|
63
|
101
|
WC wc = new WC(fullPath);
|
|
64
|
|
- wc.printWordCount(wc.readFile());
|
|
|
102
|
+ HashMap<String, Integer> result = wc.readFile();
|
|
|
103
|
+ printWordCount(sortList(result));
|
|
65
|
104
|
|
|
66
|
105
|
|
|
67
|
106
|
|