Java 如何修复HashMap更新每个现有值而不是单个值

Java 如何修复HashMap更新每个现有值而不是单个值,java,hashmap,Java,Hashmap,我一直在处理一个醉汉沃克的编码问题(自定义用户类等),试图解决这个小问题简直是疯了 我把代码弄得乱七八糟(毫无用处),所以在看不到希望的情况下,我决定征求外界的意见 我用于添加到hashmap中的代码如下所示: if (hashMap.containsKey(key) == false) { hashMap.put(key, 1); } else { hashMap.put(key, value + 1); } Visited Intersection [avenue=8, s

我一直在处理一个醉汉沃克的编码问题(自定义用户类等),试图解决这个小问题简直是疯了

我把代码弄得乱七八糟(毫无用处),所以在看不到希望的情况下,我决定征求外界的意见

我用于添加到hashmap中的代码如下所示:

if (hashMap.containsKey(key) == false) {
    hashMap.put(key, 1);
}
else {
    hashMap.put(key, value + 1);
}
Visited Intersection [avenue=8, street=42] 3 times!
Visited Intersection [avenue=8, street=63] 2 times!
Hash Map: {Intersection [avenue=6, street=22]=1}

Hash Map: {Intersection [avenue=6, street=23]=1, Intersection 
[avenue=6, street=23]=1}

Hash Map: {Intersection [avenue=6, street=22]=2, Intersection 
[avenue=6, street=22]=1}

Hash Map: {Intersection [avenue=5, street=22]=2, Intersection 
[avenue=5, street=22]=1, Intersection [avenue=5, street=22]=1}

Hash Map: {Intersection [avenue=6, street=22]=3, Intersection 
[avenue=6, street=22]=1, Intersection [avenue=6, street=22]=1}

...
理论上,这应该是完全正确的。如果密钥未保存在地图中,则会将其添加到地图中,值为1。如果键实际上在映射中,则该值将递增1。键只是一个接受两个整数变量的自定义类的实例。它正在不断更新

在程序结束时,如果我在hashmap中显示值大于1的条目,它应该如下所示:

if (hashMap.containsKey(key) == false) {
    hashMap.put(key, 1);
}
else {
    hashMap.put(key, value + 1);
}
Visited Intersection [avenue=8, street=42] 3 times!
Visited Intersection [avenue=8, street=63] 2 times!
Hash Map: {Intersection [avenue=6, street=22]=1}

Hash Map: {Intersection [avenue=6, street=23]=1, Intersection 
[avenue=6, street=23]=1}

Hash Map: {Intersection [avenue=6, street=22]=2, Intersection 
[avenue=6, street=22]=1}

Hash Map: {Intersection [avenue=5, street=22]=2, Intersection 
[avenue=5, street=22]=1, Intersection [avenue=5, street=22]=1}

Hash Map: {Intersection [avenue=6, street=22]=3, Intersection 
[avenue=6, street=22]=1, Intersection [avenue=6, street=22]=1}

...
但是当我观察到hashmap在每个函数调用中的样子时,它看起来是这样的:

if (hashMap.containsKey(key) == false) {
    hashMap.put(key, 1);
}
else {
    hashMap.put(key, value + 1);
}
Visited Intersection [avenue=8, street=42] 3 times!
Visited Intersection [avenue=8, street=63] 2 times!
Hash Map: {Intersection [avenue=6, street=22]=1}

Hash Map: {Intersection [avenue=6, street=23]=1, Intersection 
[avenue=6, street=23]=1}

Hash Map: {Intersection [avenue=6, street=22]=2, Intersection 
[avenue=6, street=22]=1}

Hash Map: {Intersection [avenue=5, street=22]=2, Intersection 
[avenue=5, street=22]=1, Intersection [avenue=5, street=22]=1}

Hash Map: {Intersection [avenue=6, street=22]=3, Intersection 
[avenue=6, street=22]=1, Intersection [avenue=6, street=22]=1}

...
hashmap中的每个条目都被覆盖,最终结果如下:

Visited Intersection [avenue=8, street=20] 3 times!
Visited Intersection [avenue=8, street=20] 2 times!
Visited Intersection [avenue=8, street=20] 2 times!
Visited Intersection [avenue=8, street=20] 2 times!
...
最初我认为添加到hashmap的代码是不正确的,因为每个键都被覆盖,并且只显示最后更新的一个,但现在我认为这与实际更新的键有关

你的想法怎么样?抱歉,如果有点模糊

hashmap中的每个条目都被覆盖

我怀疑您不太了解
HashMap
的工作原理
HashMap
存储对
键的引用,而不是副本。我怀疑您在将
交叉点
放入地图后,正在覆盖该交叉点中的字段。这是一个非常糟糕的模式,可能会导致一些非常奇怪的结果

有几件事需要检查

  • 每次你都应该做一个
    新的十字路口(大街、街道)
  • 考虑将
    交叉点中的两个字段设置为
    最终
    。这始终是一个好模式,因此不会无意中更改键的值。当然,如果一个或两个字段都是“标识”字段,那么它应该是
    final
  • 您需要确保
    交叉点
    对象具有正确标识每个值的适当的
    hashcode()
    equals()
    方法。否则,每个
    交叉口
    将存储在地图中,无论它们是否具有相同的
    大道
    街道
    值。请看我的回答:
  • 您应该从地图中获取交点的计数,然后递增该值
可能是这样的:

Intersection key = new Intersection(8, 42);
...
Integer count = hashMap.get(key);
if (count == null) {
   hashMap.put(key, 1);
} else {
   hashMap.put(key, value + 1);
}
...
public class Intersection {
   // these fields can't be changed
   private final int avenue;
   private final int street;
   ...

hashMap.put(键,值+1)是的。从技术上讲,值只是hashMap。get(key)我不理解你的问题。你想要什么?你的重点课程看起来怎么样?那么你的价值是什么呢?除了你实际的bug之外:(1)不要用布尔型的
=
;使用
!map.containsKey(键)
。(2)
map.merge(键、值、v->v+1)