-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreqWordsCount.java
59 lines (45 loc) · 1.63 KB
/
FreqWordsCount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package youtube;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class FreqWordsCount {
public static ArrayList<String> counter (ArrayList<String> wordList){
ArrayList<String> freqWordList = new ArrayList<String>();
Map<String, Integer> map = new HashMap<>();
for (String word : wordList) {
if (!word.isEmpty()) {
if (map.containsKey(word)) {
int count = map.get(word) + 1;
map.put(word, count);
} else {
map.put(word, 1);
}
}
}
ArrayList<String> list = new ArrayList<>();
int maxLengthOfSpelling = 0;
for (String key : map.keySet()) {
list.add(key);
if (maxLengthOfSpelling < key.length()) {
maxLengthOfSpelling = key.length();
}
}
Collections.sort(list, (o1, o2) -> {
return - map.get(o1) + map.get(o2);
});
// 3回以上出た名詞 上位から
// System.out.println("2回以上出た名詞"
// + "");
// String format = "%-" + maxLengthOfSpelling + "s: %3d";
for (String word : list) {
int count = map.get(word);
if (3 <= count && freqWordList.size()<3) {
//System.out.printf(format, word, count);
//System.out.println();
freqWordList.add(word);
}
}
return freqWordList;
}
}