File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with hashCode() and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with hashCode() and equals()" Watch "problem with hashCode() and equals()" New topic
Author

problem with hashCode() and equals()

saipavan vallabhaneni
Ranch Hand

Joined: Nov 14, 2008
Posts: 34

though i had over ridden the hashCode() and equals() method i am unable to retrieve the stored value... y is it soo?
saipavan vallabhaneni
Ranch Hand

Joined: Nov 14, 2008
Posts: 34
new Person("sai")..the hashCode returns 3 and so in the corresponding bucket using equals method search for "sai" should be made and the corresponding value "11" must b retrieved ..but i am not getting it
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

just look at the equals method again...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
public boolean equals(Person p) {
return name.equals(p.name);
}


This is wrong overriding.

You have to ride.
public boolean equals(Object p) {
return name.equals(((Person)p).name);
}

[ December 17, 2008: Message edited by: punit singh ]

SCJP 6
saipavan vallabhaneni
Ranch Hand

Joined: Nov 14, 2008
Posts: 34
thanks ankit and punit
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Duc Vo you can delete your post if you don't want it. Just edit it and on the edit screen there is a check box which says delete this post. Check it and then press the edit post button...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with hashCode() and equals()