java 计算Hashmap中的重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27254302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting duplicate values in Hashmap
提问by user2379090
I have a hash map that looks like this:
我有一个看起来像这样的哈希图:
HashMap<String, ArrayList<String>> varX = new HashMap<String, ArrayList<String>>();
And I can't for the life of me work out how to count the number of duplicate values.
For example, If put("001", "DM");
into the hash map and put("010", "DM");
as well, how can count if there are two values int the ArrayList section of the Hashmap.
而且我一生都无法弄清楚如何计算重复值的数量。例如,如果put("001", "DM");
进入哈希映射,put("010", "DM");
那么如何计算哈希映射的 ArrayList 部分是否有两个值。
For example, the output would look something like this:
例如,输出将如下所示:
DM:2
as I 'put' two DM values into the Hashmap.
DM:2
当我将两个 DM 值“放入”Hashmap 时。
回答by nem035
You have a HashMap that maps String
to ArrayList<String>
.
你有一个 HashMap 映射String
到ArrayList<String>
.
Doing put("001", "DM")
on this map will not work as was pointed out to you in the comments by @Sotirios Delimanolis.
做put("001", "DM")
这个地图将无法正常工作的指出你在所作的评论@Sotirios Delimanolis。
You would get an error that looks like:
你会得到一个看起来像这样的错误:
The method put(String, ArrayList<String>) in the type HashMap<String,ArrayList<String>> is not applicable for the arguments (String, String)
Based on your example behavior, you want a HashMap
that maps String
to String
(i.e. put("001", "DM");
根据您的示例行为,您想要一个HashMap
映射String
到String
(即put("001", "DM");
Now, assuming you have that:
现在,假设你有:
HashMap<String, String> varX = new HashMap<String, String>();
And you want to count how many keys map to the same value, here's how you can do that:
并且您想计算有多少键映射到相同的 value,您可以这样做:
varX.put("001", "DM");
varX.put("010", "DM");
// ...
int counter = 0;
String countingFor = "DM";
for(String key : varX.keySet()) { // iterate through all the keys in this HashMap
if(varX.get(key).equals(countingFor)) { // if a key maps to the string you need, increment the counter
counter++;
}
}
System.out.println(countingFor + ":" + counter); // would print out "DM:2"
回答by Saravanan R
HashMap hm =new HashMap();
hm.put("01","one");
hm.put("02","two");
hm.put("03","one");
hm.put("04","one");
hm.put("05","two");
HashMap newHm = new HashMap();
Iterator it = hm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
if(newHm.containsKey(pair.getValue())){
newHm.put(pair.getValue(), Integer.parseInt(newHm.get(pair.getValue()).toString())+1 );
}else{
newHm.put(pair.getValue(),1 );
}
it.remove(); // avoids a ConcurrentModificationException
}
回答by Imdad Soomro
Collections.frequency(map, "value");
is used to count the passed object in collection.
Following is the declaration of that method:
Collections.frequency(map, "value");
用于统计集合中传递的对象。以下是该方法的声明:
public static int frequency(Collection<?> c, Object o)
回答by MartaGom
As Sotirios says, you can only put an ArrayList.
正如 Sotirios 所说,您只能放置一个 ArrayList。
ArrayList<String> names= new ArrayList<String>();
names.add("Josh");
put("001", names);
If you want to insert Strings into the HashMap, define it as follow:
如果要将字符串插入到 HashMap 中,请定义如下:
Map<String,String> example = new HashMap<String,String>();
example.put( "001", new String( "Rooney" ));
Regards,
问候,