|
@@ -6,6 +6,7 @@ import java.util.HashMap;
|
6
|
6
|
import java.util.Iterator;
|
7
|
7
|
import java.util.Map;
|
8
|
8
|
import java.util.Scanner;
|
|
9
|
+import java.util.stream.Stream;
|
9
|
10
|
|
10
|
11
|
public class WC {
|
11
|
12
|
private Iterator<String> si;
|
|
@@ -26,30 +27,28 @@ public class WC {
|
26
|
27
|
this.si = si;
|
27
|
28
|
}
|
28
|
29
|
|
29
|
|
- public void wordCount(){
|
30
|
|
- String words;
|
31
|
|
-
|
32
|
|
- while (si.hasNext()){
|
33
|
|
- String lowerCaseWords =si.next().toLowerCase();
|
34
|
|
- words = lowerCaseWords.replaceAll("[^a-z]", "");
|
|
30
|
+ public void wordCount() {
|
|
31
|
+ String words;
|
|
32
|
+int count=0;
|
35
|
33
|
|
36
|
|
- if (countTheWords.containsKey(words)){
|
37
|
|
- countTheWords.put(words, countTheWords.get(words) + 1 );
|
38
|
|
- } else{
|
39
|
|
- countTheWords.put(words, 1);
|
|
34
|
+ while (si.hasNext()) {
|
|
35
|
+ String lowerCaseWords = si.next().toLowerCase();
|
|
36
|
+ words = lowerCaseWords.replaceAll("[^a-z]" ,"");
|
40
|
37
|
|
41
|
|
- }
|
|
38
|
+ if (countTheWords.containsValue(words)) {
|
|
39
|
+ countTheWords.put(words, countTheWords.get(words) + 1 );
|
|
40
|
+ } else {
|
|
41
|
+ countTheWords.put( words, count++);
|
|
42
|
+
|
|
43
|
+ }
|
42
|
44
|
}
|
43
|
|
- countTheWords.entrySet().stream().
|
44
|
|
- sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
|
45
|
45
|
|
46
|
|
- .forEach (entry -> System.out.println (String.format("%15s , %d\n ", entry.getValue(), entry.getKey() )));
|
47
|
46
|
}
|
48
|
|
- public void reverseAndPrint(String fileName){
|
49
|
|
- countTheWords.entrySet().stream().
|
50
|
|
- sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
|
|
47
|
+ public Stream<Map.Entry<String, Integer>> reverse(String fileName){
|
|
48
|
+ return countTheWords.entrySet().stream()
|
|
49
|
+ .sorted(Map.Entry.<String, Integer>comparingByValue().reversed());
|
|
50
|
+
|
51
|
51
|
|
52
|
|
- .forEach (entry -> System.out.println (String.format("%15s , %d\n ", entry.getValue(), entry.getKey() )));
|
53
|
52
|
|
54
|
53
|
}
|
55
|
54
|
|