| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jackylee
10年前发布

Java 使用 Collections.frequency 找出重复的单词

frequency 是 Collections 的一个方法,可找出一个单词在list中出现的次数

[Java]代码

@SuppressWarnings("unchecked")  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));  }  }

执行结果

ft: 1  f: 7  g: 2  d: 5  e: 1  b: 1  c: 1  a: 3  wed: 1  sd: 1  se: 1  j: 3  ws: 1  k: 2  h: 2  w: 1  v: 1  s: 4  r: 1  gh: 1  x: 1