You're looking up a value in the map that has a new Cat object as its key. That new Cat object you're using as the key is ofcourse a different new Cat object than the one you use in line 18 to put the value in the map.
Since your class Cat (line 8) does not have equals() and hashCode() methods, it uses the equals() and hashCode() from class Object, which use == and return a unique hash code for every instance. So, if you have to separate Cat objects, they will not be equal.
You need to override equals() and hashCode() methods in Cat class because Hashvalue of object should be match with the key passed in map.since new Cat() will create new Object in every call , it does not guarantee the same hash value.so the value for this key is returned as NULL.
subodh k kumar wrote:You need to override equals() and Hashcode() methods in CAT class because Hashvalue of object should be match with the key passed in map.since new Cat() will create new Object in every call , it does not guarantee the same hash value.so the value for this key is returned as NULL.
You mean the equals() and hashCode() method, as Jesper explained.
always annotate with @Override and save yourself a heap of sadness.
Catherine austin
Ranch Hand
Joined: Jul 20, 2011
Posts: 43
posted
0
Can we use indexof() in this example to refer to refer to objects ?
If yes then how?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Catherine austin wrote:Can we use indexof() in this example to refer to refer to objects ?
If yes then how?
I don't think a Map has an indexOf() method. That is only appropriate for Lists. Maps don't store their contents by index. Have a loop at the interfaces in the Java™ Tutorials.
Catherine austin
Ranch Hand
Joined: Jul 20, 2011
Posts: 43
posted
0
Oh yeah i got you . Thanks for clarification.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.