• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

non duplicates in a Set

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am in the process of writing a small program where I have created a very simple class called Vertex that has a String as a field. I am now attempting to add a number of instances of my Vertex class into a HashSet. However, my resultant HashSet has duplicates in it.

I have added a equals method and compareTo method in my Vertex class to attempt to allow my HashSet to uniquely identify Vertexes but I am clearly missing something. Can anyone point me in the right direction. Do I need to override the HashCode in my Vertex class?








This results in a Set of [A, B, A, C] when I would like to have no duplicates, hence my choice in using a Set.

Thanks in advance
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob McBryde wrote:Do I need to override the HashCode in my Vertex class?


Yes, that's it. You should always override hashCode() whenever you override equals(), as they should be consistent with each other (see the contract descriptions in the Javadocs for Object). HashSet relies on hashCode() working correctly (the "Hash" in the name is not a coincidence!).
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And your equals method has a huge bug. If you pass null or an instance of an incompatible class the method should return false. Your implementation will throw a NullPointerException / ClassCastException.
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not only is it bugged, it uses a poor construct:
 
Rob McBryde
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice, I have re-written my equals method and overridden the hashCode() method within my Vector class. Have tested this and no duplicate Vectors are being added to my HashSet. Hurray!






I appreciate the comments, a little unsure about Stephan's comment about the poor construct of my initial equals method. Would you be able to further advise why it is poor and what I should be doing to improve it ?

Thanks
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what Stephan is getting at is that you can replace:


with:

thus improving the readability of your code.
 
Rob McBryde
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,

Makes perfect sense to me now. I've updated my code and will I ensure I stick to the more readable and less verbose approach from now on.

 
reply
    Bookmark Topic Watch Topic
  • New Topic