TreeMap randomly stops properly returning values?

In response to your note: everyone is asking if you've overridden equals() and hashcode() properly -- which is important, yes. But this is a TreeMap which means you also have to care about comparisons -- whether you're using Comparable objects or an external Comparator you need to make sure that your comparisons are consistent (are your objects mutable? ) and that they're consistent with your equals() method.

In response to your note: everyone is asking if you've overridden equals() and hashcode() properly -- which is important, yes. But this is a TreeMap, which means you also have to care about comparisons -- whether you're using Comparable objects or an external Comparator, you need to make sure that your comparisons are consistent (are your objects mutable? ) and that they're consistent with your equals() 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.

I see your point about the mention of String objects, sorry about that. As for your comment, I'm not sure I follow. What do you mean "make sure your comparisons are consistent"... make sure that my objects aren't changing without me be aware of it?

If that's the case, I also feel fairly confident that the objects themselves are not changing in any way. The code this is all in is for sorting amongst the objects, but not for operating on them, if that makes sense. – smessing Apr 21 '10 at 3:46.

If you're literally using distances. Get(Face), that means you're trying to pass in a variable Face, and it's not surprising that you're getting null 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 variable Face that might contain a string.

Sorry, this needs clarification: in my actual code I'm using different objects (my own data type), so the confusion you speak of can't be what's happening. – smessing Apr 21 '10 at 3:14.

This code works for me: TreeMap map = new TreeMap(); map. Put("a", 1); map. Put("b", 2); System.out.

Println(map); System.out. Println(map. Get("a")); System.out.

Println(map. Get("b")); Will output {a=1, b=2} 1 2.In your case, if Foo and Face are not both Strings, the map can not use them as keys for a lookup. Maybe you are using get(Face) where you should use get("Face")?

See comment on other answer, but basically, I'm not actually using Strings, I'm using a different data type. Strings were for illustrative purposes. – smessing Apr 21 '10 at 3:15.

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.

I have implemented both, and feel fairly confident that they aren't the problem. Also, remember that for a while the get() calls return the proper values, but at some point stop... – smessing Apr 21 '10 at 3:28 -1 - hashCode is irrelevant for a TreeMap! – Stephen C Apr 21 '10 at 4:20.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions