Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with hashCode() and equals()

 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9514
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just look at the equals method again...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ankit and punit
 
Ankit Garg
Sheriff
Posts: 9514
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic