计算 java 中 ArrayList 中字符串的出现次数 - 处理
Counting occurrences of strings in an ArrayList in java - Processing
你好,我目前正在使用处理和学习 java,我的代码基本上通过 ArrayList 工作,找到出现次数最多的单词并将其输出到控制台,我的代码如下:
import java.util.Arrays;
ArrayList<String> words = new ArrayList();
int[] occurrence = new int[2000];
void setup() {
size(800,480);
smooth();
String[] data = loadStrings("data/data.txt");
Arrays.sort(data);
for (int i = 0; i < data.length; i ++ ) {
words.add(data[i]);
words.add(data[j]); //Put each word into the words ArrayList
}
for(int i =0; i<data.length; i++) {
occurrence[i] =0;
for(int j=i+1; j<data.length; j++) {
if(data[i].equals(data[j])) {
occurrence[i] = occurrence[i]+1;
}
}
}
int max = 0;
String most_talked ="";
for(int i =0;i<data.length;i++) {
if(occurrence[i]>max) {
max = occurrence[i];
most_talked = data[i];
}
}
println("The most talked keyword is " + most_talked + " occuring " + max + " times.");
我想知道我将如何改变它以添加第二个最常出现的词,依此类推。
我研究过使用地图以及 collection.sort,但不太了解如何继续使用它。我是 java 的新手,所以任何东西都会有所帮助。
我首先想到的是将使用过的单词保存在一个辅助数组中,然后对于每个查询的单词,在这个列表中找到它。
如果匹配增加这个词的计数器(如果有太多你也可以添加一个int []来存储出现次数)然后只显示它(每个aux [index]与Occurrence [index] ).
Example: (Only a scheme)
If the list is:
Tom Tom Dog fish
Then:
Aux[0] = Tom;
Aux[1] = Dog;
Aux[3] = fish;
并且每个出现在 "int list" 中:对于 Tom 索引 = 0,
狗 = 1 和鱼 = 3。
希望对您有所帮助!
Guava 库中的 Multisets 似乎非常适合这项工作。您可以将您读过的所有单词存储到 Multiset
中,当您想要计算出现次数(计数)时,您可以简单地遍历 Multisets.copyHighestCountFirst(myMultiset)
:
返回的副本
import com.google.common.collect.*;
...
// data contains the words from the text file
Multiset<String> myMultiset = ImmutableMultiset.copyOf(data);
for (String word : Multisets.copyHighestCountFirst(myMultiset).elementSet()) {
System.out.println(word + ": " + myMultiset.count(word));
}
应该这样做。
你好,我目前正在使用处理和学习 java,我的代码基本上通过 ArrayList 工作,找到出现次数最多的单词并将其输出到控制台,我的代码如下:
import java.util.Arrays;
ArrayList<String> words = new ArrayList();
int[] occurrence = new int[2000];
void setup() {
size(800,480);
smooth();
String[] data = loadStrings("data/data.txt");
Arrays.sort(data);
for (int i = 0; i < data.length; i ++ ) {
words.add(data[i]);
words.add(data[j]); //Put each word into the words ArrayList
}
for(int i =0; i<data.length; i++) {
occurrence[i] =0;
for(int j=i+1; j<data.length; j++) {
if(data[i].equals(data[j])) {
occurrence[i] = occurrence[i]+1;
}
}
}
int max = 0;
String most_talked ="";
for(int i =0;i<data.length;i++) {
if(occurrence[i]>max) {
max = occurrence[i];
most_talked = data[i];
}
}
println("The most talked keyword is " + most_talked + " occuring " + max + " times.");
我想知道我将如何改变它以添加第二个最常出现的词,依此类推。
我研究过使用地图以及 collection.sort,但不太了解如何继续使用它。我是 java 的新手,所以任何东西都会有所帮助。
我首先想到的是将使用过的单词保存在一个辅助数组中,然后对于每个查询的单词,在这个列表中找到它。
如果匹配增加这个词的计数器(如果有太多你也可以添加一个int []来存储出现次数)然后只显示它(每个aux [index]与Occurrence [index] ).
Example: (Only a scheme)
If the list is:
Tom Tom Dog fish
Then:
Aux[0] = Tom;
Aux[1] = Dog;
Aux[3] = fish;
并且每个出现在 "int list" 中:对于 Tom 索引 = 0, 狗 = 1 和鱼 = 3。
希望对您有所帮助!
Guava 库中的 Multisets 似乎非常适合这项工作。您可以将您读过的所有单词存储到 Multiset
中,当您想要计算出现次数(计数)时,您可以简单地遍历 Multisets.copyHighestCountFirst(myMultiset)
:
import com.google.common.collect.*;
...
// data contains the words from the text file
Multiset<String> myMultiset = ImmutableMultiset.copyOf(data);
for (String word : Multisets.copyHighestCountFirst(myMultiset).elementSet()) {
System.out.println(word + ": " + myMultiset.count(word));
}
应该这样做。