• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashSet Question?  RSS feed

 
Dairmon Lee
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I have question on HashSet, I created my own class and override the equals(), but it not working as I expected while I added new classes into HashSet, anybody could tell me what is wrong here?
Here is the Code:

It suppose to only add the first element, but 2 element was added??
What I missing here???
Thank you in advance!!!
(edited by Cindy to use the [ code] [ /code] tags - html is turned off)
[ December 18, 2002: Message edited by: Cindy Glass ]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to override hashCode() as well. From the java.lang.Object.hashCode() javadoc: If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
- Peter
 
Dairmon Lee
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much, it's working right now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!