我有十个带有单词的数组列表。我想创建另一个包含2个字段的数组列表,word,int =发生了多少个数组列表。
ArrayList<String> text1;
ArrayList<String> text2;
ArrayList<String> text3;
ArrayList<String> text4;
ArrayList<String> text5;
ArrayList<String> text6;
ArrayList<String> text7;
ArrayList<String> text8;
ArrayList<String> text9;
ArrayList<String> text10;
我想要像
这样的结果apple 3
banana 8
cucumber 5
...
答案 0 :(得分:1)
如果您使用的是Java8,则可以使用Collectors#groupingBy()
收集器:
Map<String, Long> result =
Stream.of(text1, text2, text3 .... text10)
.flatMap(list -> list.stream())
.collect(Collectors.groupingBy(x -> x, Collectors.counting()));
如果您运行的是Java8之前的版本,则需要遍历每个列表并将每个元素添加到Map<String, Integer>
。操作其中一个列表的简短示例:
Map<String, Integer> result = new TreeMap<String, Integer>();
for (String x : text1) {
if (!result.containsKey(x)) {
result.put(x, 1);
} else {
Integer count = result.get(x);
result.put(x, ++count);
}
}
答案 1 :(得分:1)
//将数组放入向量中并使用其索引遍历ArrayLists并计算单词的出现次数。像这样:
import java.util.Vector;
import java.util.ArrayList;
public class WordCount{
private static Vector<ArrayList<String>> stringArrays = new Vector<ArrayList<String>>();
public static void main(String[] args){
ArrayList<String> text1 = new ArrayList<String>();
ArrayList<String> text2 = new ArrayList<String>();
ArrayList<String> text3 = new ArrayList<String>();
ArrayList<String> text4 = new ArrayList<String>();
ArrayList<String> text5 = new ArrayList<String>();
ArrayList<String> text6 = new ArrayList<String>();
ArrayList<String> text7 = new ArrayList<String>();
ArrayList<String> text8 = new ArrayList<String>();
ArrayList<String> text9 = new ArrayList<String>();
ArrayList<String> text10 = new ArrayList<String>();
stringArrays.add(text1);
stringArrays.add(text2);
stringArrays.add(text3);
stringArrays.add(text4);
stringArrays.add(text5);
stringArrays.add(text6);
stringArrays.add(text7);
stringArrays.add(text8);
stringArrays.add(text9);
stringArrays.add(text10);
stringArrays.get(0).add("apple");
stringArrays.get(0).add("cucumber");
stringArrays.get(1).add("apple");
stringArrays.get(1).add("cucumber");
stringArrays.get(2).add("apple");
stringArrays.get(2).add("banana");
stringArrays.get(2).add("cucumber");
stringArrays.get(3).add("banana");
stringArrays.get(4).add("banana");
stringArrays.get(5).add("banana");
stringArrays.get(6).add("banana");
stringArrays.get(7).add("banana");
stringArrays.get(7).add("cucumber");
stringArrays.get(8).add("banana");
stringArrays.get(9).add("banana");
stringArrays.get(9).add("cucumber");
System.out.println("apple " + countWord("apple"));
System.out.println("banana " + countWord("banana"));
System.out.println("cucumber " + countWord("cucumber"));
}//end main
// Count the words
private static int countWord(String findWord){
boolean wordFound;
int wordCount = 0;
for( int i = 0; i < stringArrays.size(); ++i ){
wordFound = false;
for ( int j = 0; j < stringArrays.get(i).size() && !wordFound; ++j ){
if ( stringArrays.get(i).get(j).equals(findWord) ) wordFound = true;
}if( wordFound ) ++wordCount;
}
return wordCount;
}
}//end WordCount class
答案 2 :(得分:0)
创建一个循环/迭代器,然后将其放入循环中并使用您的过程
Map<String, Integer> map = new TreeMap<String, Integer>();
int numberOfOccurrence = 0;
String yourWord = "";
for(){
//Process your list and extract out the word and the number of occurrence the put it on your Map.
//this is the key, and this is the value.
map.put(yourWord, numberOfOccurrence);
}
你的单词作为一个键,并将出现的数量作为一个值。
答案 3 :(得分:0)
使用Map<String, Integer>
键作为条目名称,使用值作为计数。
Map<String, Integer> bucket = new HashMap<String, Integer>();
//you can aggregrate (join) all list as a single list here
for (String entry : lists) { //need to iterate all of your lists
int count = 1;
if (bucket.containsKey(entry)) {
count = bucket.get(entry) + 1;
}
bucket.put(entry, count);
}
//here bucket is fulfilled with entry name & count