frequency 是 Collections 的一个方法,可找出一个单词在list中出现的次数
源码:
package com.mfl.demo;
import java.util.ArrayList;import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class QueryRepetition {
/**
* @param args
*/
public static void main(String[] args) {
String text = "a r b k c d se f g a d f s s f d s ft gh f ws w f v x s g h d h j j k f sd j e wed a d f";
List<String> list = new ArrayList<String>();
list.addAll(Arrays.asList(text.split(" ")));
Set<String> uniqueWords = new HashSet<String>(list);
for (String word : uniqueWords) {
System.out.println(word + ": " + Collections.frequency(list, word));
}
}
}
运行结果:
wed: 1
ws: 1
k: 2
h: 2
c: 1
ft: 1
v: 1
a: 3
j: 3
s: 4
se: 1
d: 5
w: 1
r: 1
sd: 1
g: 2
b: 1
gh: 1
f: 7
x: 1
e: 1