[英]Increment of value in Hashmap<Object,Integer>, Error "Integer cannot be converted to P(object)" Java
I'm trying to increment of value in hashmap.我正在尝试增加哈希图中的值。 I'm using Java 8 and Netbeans, so wrote this code:
我正在使用 Java 8 和 Netbeans,所以写了这段代码:
Map<P, Integer>color_match_hash = new HashMap<P, Integer>();
for(int k=0; k<color_match_hash.size(); k++){
if(color_match_hash.get(k).equals(P_Color.get(i))){
color_match_hash.merge(color_match_hash.get(k), 1, Integer::sum);
}
} }
P is an object with 5 Integers. P 是一个有 5 个整数的对象。 It is a simple algorithm, in which I want to increment value of key (k) and I got "incompatible types: Integer cannot be converted to P".
这是一个简单的算法,我想在其中增加键 (k) 的值并且得到“不兼容的类型:整数无法转换为 P”。 What I'm doing wrong?
我做错了什么? I must add that I tried a lot of ways to increment this and in every I had the same message.
我必须补充一点,我尝试了很多方法来增加它,并且在每个方法中我都有相同的消息。 Most efficient way to increment a Map value in Java
在 Java 中增加 Map 值的最有效方法
You have created a HashMap
with key of type P
and value of type Integer
.您已经创建了一个
HashMap
,其键类型为P
,值类型为Integer
。
First of all, you are iterating the map like we do for a list or array.首先,您像我们对列表或数组所做的那样迭代地图。
Secondly, this is a wrong usage - color_match_hash.get(k)
.其次,这是一个错误的用法 -
color_match_hash.get(k)
。 The get
method expects a value having a data type P
(the key). get
方法需要一个数据类型为P
(键)的值。
You can read How to efficiently iterate over each entry in a Java Map?您可以阅读如何有效地迭代 Java Map 中的每个条目? , to see different ways to iterate over a map.
, 查看迭代地图的不同方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.