TreeMap 随机停止正确返回值
我有以下 TreeMap:
TreeMap<String, Integer> distances = new TreeMap<String, Integer>();
它包含两个字符串“Face”和“Foo”,并具有适当的值,例如:
System.out.println(distances);
产量:
{Face=12, Foo=2}
但是,distances.get(Face)
返回 null< /code>,即使
distances.get(Foo)
正确返回 2。以前,distances.get(Face)
可以工作,但由于某种原因,它停止工作。请注意,在为两个键调用 get() 之前,我正确打印出地图,因此我没有意外地将 Face 的值更改为 null。还有其他人遇到过这个问题吗?有什么我可以做的吗?我只是想弄清楚如何调试这个问题,这让我度过了一段糟糕的时光。
注意:在实际代码中,我实际上并没有使用字符串,而是使用不同的对象,因此它是:TreeMap
。因此,这不仅仅是变量名与文字字符串的混淆。
第二条注意:我对我正在使用的对象的 hashcode()
和 equals()
实现也非常有信心。 (另外,如果我的实现不正确,那不是从一开始就无法工作吗?而不是随机停止工作?)
I have the following TreeMap:
TreeMap<String, Integer> distances = new TreeMap<String, Integer>();
and it contains both strings, "Face" and "Foo", with appropriate values, such that:
System.out.println(distances);
Yields:
{Face=12, Foo=2}
However, distances.get(Face)
returns null
, even though distances.get(Foo)
properly returns 2. Previously, distances.get(Face)
worked, but for some reason, it stopped working. Note I print out the map right before calling get() for both keys, so I haven't accidentally changed Face's value to null. Has anyone else ever encountered this problem? Is there anything I can do? I'm having a terrible time simply trying to figure out how to debug this problem.
NOTE: In the real code I'm not actually using Strings, but a different object, so it's: TreeMap<Object, Integer>
. So it's not simply a confusion of variable names vs. literal strings.
SECOND NOTE: I also feel pretty confident about my implementations of hashcode()
and equals()
for the object I'm using. (Also, if my implementations weren't correct, wouldn't it not work from the beginning? Instead of stopping to work randomly?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
回应你的注释:每个人都在问你是否正确地重写了 equals() 和 hashcode() ,这很重要,是的。但这是一个
TreeMap
,这意味着您还必须关心比较 - 无论您使用的是Comparable
对象还是外部Comparator
,您需要确保您的比较是一致的(您的对象是否可变?)并且它们与您的equals()
方法一致。顺便说一句,当您在最初的问题中说您正在使用
String
对象时,您对自己造成了伤害 - 字符串是不可变的,并且这里不考虑它们的比较方法,因此问题根本不同;既然我们知道涉及到您自己的代码,那么可能的解决方案的领域就更广泛了。In response to your note: everyone is asking if you've overridden
equals()
andhashcode()
properly -- which is important, yes. But this is aTreeMap
, which means you also have to care about comparisons -- whether you're usingComparable
objects or an externalComparator
, you need to make sure that your comparisons are consistent (are your objects mutable?) and that they're consistent with yourequals()
method.Incidentally, when you said in your original question that you were using
String
objects, you did yourself a disservice -- Strings are immutable and their comparison methods aren't under consideration here, so the question was fundamentally different; now that we know your own code is involved, the field of possible solutions is wider.您是否将字符串文字
"Face"
与名为Face
的变量混淆了?如果您确实使用distances.get(Face)
,则意味着您正在尝试传入变量Face
,并且您得到Face
并不奇怪。 code>null 返回(除非这是一个字符串变量,其内容是您之前添加到地图中的)。相反,请尝试
distances.get("Face")
。如果有效,则意味着"Face"
是您用作键的字符串 - 它与可能包含字符串的变量Face
不同。Are you confusing the string literal
"Face"
with a variable calledFace
? If you're literally usingdistances.get(Face)
, that means you're trying to pass in a variableFace
, and it's not surprising that you're gettingnull
back out (unless that's a String variable whose contents you've previously added to the map).Instead try
distances.get("Face")
. If that works, it means"Face"
is the string you're using as a key -- which isn't the same as a variableFace
that might contain a string.您确定值“Foo”和“Face”都是
字符串
吗?此代码对我有用:
将输出 {a=1, b=2}
1
2.
在您的情况下,如果
Foo
和Face
不都是String
,则地图无法将它们用作查找的键。也许您在应该使用get("Face")
的地方使用了get(Face)
?Are you sure that the values "Foo" and "Face" are both
Strings
?This code works for me:
Will output {a=1, b=2}
1
2.
In your case, if
Foo
andFace
are not bothString
s, the map can not use them as keys for a lookup. Maybe you are usingget(Face)
where you should useget("Face")
?@smessing 您是否在对象 Foo 和 Face 所属的类上正确实现了 equals 和 hashcode 方法?
当您想要将对象存储在列表、映射或集合中时,需要实现 equals 和 hashCode,以便它们遵守文档中指定的标准约定。
如果这些没有正确完成,get 操作可能会导致意外结果,如您所提到的。
@smessing did you properly implement
equals
andhashcode
methods on your class that objects Foo and Face belong to?When you want to store an object in a List, Map or a Set then it is a requirement that equals and hashCode are implemented so they obey the standard contract as specified in the documentation.
If these are not properly done, a get operation can result in unexpected results as you mentioned.
如果您将 Face 分配给其他参考对象/变量,请检查它是否变为 null。
如果您的 Face 对象指向另一个对象的引用,而另一个对象将其设置为 null,则 Face 也会变为 null。
If you are assigning Face with some other reference object/variable, check if that is becoming null.
If your face object is pointing to the reference of another object and the other object sets it to null, Face also becomes null.