我需要确定特定列表的频率计数。我的列表看起来像
gun,bun
bun,gun,
pin,bin
bin,pin
stay,way.
从上面的列表中,我需要输出为
gun,bun 2
pin,bin 2
stay,way 1
请提出任何想法。
答案 0 :(得分:2)
//this is data string
String str = "gun,bun,bun,gun,pin,bin bin,pin,stay,way";
// here we have created a map with String key and Integer values
Map<String, Integer> hm = new HashMap<String, Integer>();
//now we are iterating through each string by splitting data by "," so we'll get each string
for (String strTmp : str.split(",")) {
//checking if map already contains the entry then update the count
if (hm.containsKey(strTmp)) {
Integer val = hm.get(strTmp);
val = val + 1;
hm.put(strTmp, val);
} else {//else just add it
hm.put(strTmp, 1);
}
}
//printing the result
System.out.println(hm);
答案 1 :(得分:2)
您还应该查看google guava library,特别是Iterables频率方法
答案 2 :(得分:1)
使用HashMap<String,Integer>
,循环显示单词列表并存储计数。请参阅此处有关如何使用HashMap
:
http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
答案 3 :(得分:0)
为每行创建一个单词列表,并使用方法Collection.containsAll(...)来检查行是否包含相同的单词集。
答案 4 :(得分:0)
Commons Collection提供了一个“Bag”,其行为符合要求。 http://commons.apache.org/collections/apidocs/org/apache/commons/collections/Bag.html 还有一个更新的sourceforge实现可以处理泛型https://sourceforge.net/projects/collections/
答案 5 :(得分:0)
您可以使用Collections.frequency()
方法计算频率。您正在传递以逗号分隔的字符串,因此我们需要创建下面给出的此类字符串的对象。创建一个类并传递您的字符串对象(逗号分隔的String对象)并覆盖equals
和hashcode
方法以识别两个字符串是否等于。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Test {
public static void main(String[] args) {
List<CommaSeperatedStringObject> list = new ArrayList<CommaSeperatedStringObject>();
list.add(new CommaSeperatedStringObject("gun,bun"));
list.add(new CommaSeperatedStringObject("bun,gun"));
list.add(new CommaSeperatedStringObject("dan,dana,dan"));
list.add(new CommaSeperatedStringObject("pin,bin"));
list.add(new CommaSeperatedStringObject("dana,dan,dan"));
list.add(new CommaSeperatedStringObject("bin,pin"));
list.add(new CommaSeperatedStringObject("stay,way"));
list.add(new CommaSeperatedStringObject("dan,dan,dana"));
Map<CommaSeperatedStringObject, Integer> map = new HashMap<CommaSeperatedStringObject, Integer>();
for (CommaSeperatedStringObject s : list) {
map.put(s, Collections.frequency(list, s)); // here you can check frequency.
}
System.out.println(map);
}
}
class CommaSeperatedStringObject {
String str1;
List<String> strList;
public CommaSeperatedStringObject(String str) {
this.str1 = str;
String[] seperate = str1.split(",");
strList = Arrays.asList(seperate);
Collections.sort(strList);
}
@Override
public boolean equals(Object obj) {
CommaSeperatedStringObject cso = (CommaSeperatedStringObject)obj;
boolean status = false;
if(this.strList.equals(cso.strList)){
status = true;
}
return status;
}
@Override
public int hashCode() {
return 1;
}
@Override
public String toString() {
return "\n"+strList.toString();
}
}