如何计算在数组列表词汇重复? [英] How to Count Repetition of Words in Array List?
问题描述
我有这些code搜索的Array名录发生,但我的问题是我如何能得到结果
出在整型事业环路的这一端,我需要在明面,可能还有另一种方式寻找
发生使用for循环了,你能帮我吗?
谢谢...
列表<串GT;名单=新的ArrayList<串GT;();
list.add(AAA);
list.add(BBB);
list.add(AAA);SET<串GT;独特=新的HashSet<串GT;(名单);
对于(字符串键:唯一){
INT accurNO = Collections.frequency(名单,关键);
的System.out.println(键+:accurNO);
}
设置独特=新的HashSet(名单);
块引用>和
Collections.frequency(名单,关键);
块引用>有太多的开销。
下面是我怎么会做到这一点。
列表<串GT;名单=新的ArrayList<串GT;();
list.add(AAA);
list.add(BBB);
list.add(AAA);地图<字符串,整数> countMap =新的HashMap<>();
对(串词:名单){
整型数= countMap.get(字);
如果(计数== NULL){
计数= 0;
}
countMap.put(字,(count.intValue()+ 1));
}的System.out.println(countMap.toString());输出
{AAA = 2,BBB = 1}
修改输出一个接一个:迭代设置地图的条目
的(输入<字符串,整数>项:countMap.entrySet()){
的System.out.println(+ entry.getKey()+的频率'为
+ entry.getValue());
}输出
'AAA'的频率为2
BBB的频率为1编辑2 无需循环
字符串字= NULL;
整数频率= NULL;字=AAA;
频率= countMap.get(字);
的System.out.println(+字+频率,'为+
(频率== NULL?0:frequency.intValue()));字=BBB;
频率= countMap.get(字);
的System.out.println(+字+频率,'为+
(频率== NULL?0:frequency.intValue()));字=富;
频率= countMap.get(字);
的System.out.println(+字+频率,'为+
(频率== NULL?0:frequency.intValue()));输出
'AAA'的频率为2
BBB的频率为1
'富'的频率为0请注意,你将永远有一个收集和你需要的提取从它的计数为一个特定的单词或那种方式。
I've these code for searching occurrence in Array-List but my problem is how I can get result out side of this for loop in integer type cause I need in out side , may be there is another way for finding occurrence with out using for loop can you help me ? thank you...
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) { int accurNO = Collections.frequency(list, key); System.out.println(key + ": " accurNO); }
解决方案Set unique = new HashSet(list);
and
Collections.frequency(list, key);
are too much overhead.
Here is how i would do it
List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("aaa"); Map<String, Integer> countMap = new HashMap<>(); for (String word : list) { Integer count = countMap.get(word); if(count == null) { count = 0; } countMap.put(word, (count.intValue()+1)); } System.out.println(countMap.toString());
Output
{aaa=2, bbb=1}
EDIT output one by one: iterate over the set of entries of the map
for(Entry<String, Integer> entry : countMap.entrySet()) { System.out.println("frequency of '" + entry.getKey() + "' is " + entry.getValue()); }
Output
frequency of 'aaa' is 2 frequency of 'bbb' is 1
EDIT 2 No need for looping
String word = null; Integer frequency = null; word = "aaa"; frequency = countMap.get(word); System.out.println("frequency of '" + word + "' is " + (frequency == null ? 0 : frequency.intValue())); word = "bbb"; frequency = countMap.get(word); System.out.println("frequency of '" + word + "' is " + (frequency == null ? 0 : frequency.intValue())); word = "foo"; frequency = countMap.get(word); System.out.println("frequency of '" + word + "' is " + (frequency == null ? 0 : frequency.intValue()));
Output
frequency of 'aaa' is 2 frequency of 'bbb' is 1 frequency of 'foo' is 0
Note that you will always have a collection and you need extract the count from it for a particular word one way or another.
这篇关于如何计算在数组列表词汇重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!