我一直在研究醉酒的沃克编码问题(自定义用户类等),并且我要疯狂地尝试解决这个小问题。
我弄乱了代码(无济于事),因此在看不到希望的情况下,我决定征求外界意见。
我用于添加到哈希图中的代码如下:
if (hashMap.containsKey(key) == false) {
hashMap.put(key, 1);
}
else {
hashMap.put(key, value + 1);
}
理论上,这应该是完全可以的。如果键未保存在地图中,则将其值添加到地图中。值为1。如果键实际上在地图中,则该值将增加1。键只是一个带有两个整数变量的自定义类的实例。它正在不断更新。
在程序结束时,如果我在哈希图中显示值大于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}
...
哈希表中的每个条目都被覆盖,最终产品是这样:
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!
...
最初,我认为添加到哈希图中的代码是不正确的,因为每个键都被覆盖,并且只显示最后一个更新的键,但是现在我认为这与键的实际更新有关。
您的想法是Penny?抱歉,如果有点含糊。
答案 0 :(得分:7)
哈希图中的每个条目都被覆盖...
我怀疑您不太了解HashMap
的工作方式。 HashMap
存储对key
的引用而不是副本。我怀疑您在将Intersection
的字段放入地图后会覆盖它们。这是一个非常错误的模式,可能会导致一些非常奇怪的结果。
要检查的事物。
new Intersection(avenue, street)
。Intersection
中的两个字段设为final
。这始终是一个好的模式,因此您不会无意间更改键的值。确保一个或两个字段均为“身份”字段,则应为final
。Intersection
对象具有正确标识每个值的适当的hashcode()
和equals()
方法。否则,每个Intersection
都将存储在映射中,无论它们具有相同的avenue
和street
值。在这里查看我的答案:https://stackoverflow.com/a/9739583/179850 也许像这样:
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;
...