算上ArrayList的话OCCURENCES [英] Count occurences of words in ArrayList

查看:179
本文介绍了算上ArrayList的话OCCURENCES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有重复的条目文字的的ArrayList

I have an ArrayList of words with duplicate entries.

我要计数,并保存为发生在一个数据结构中的每个字。

I want to count and save occurrences for each word in a data structure.

我该怎么办呢?

推荐答案

如果你还没有大的字符串列表来实现使用Collections.frequency方法的最短途径:

If you haven't big Strings list the shortest way to implement is using Collections.frequency method:

List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");

Set<String> unique = new HashSet<String>(list);
for (String key : unique) {
    System.out.println(key + ": " + Collections.frequency(list, key));
}

输出:

aaa: 2
bbb: 1

这篇关于算上ArrayList的话OCCURENCES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆