• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

making Sets work with a custom data-type

 
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class that I made which is like a custom data-type, and I'm trying to figure out how to tell java what makes each object unique for the purposes of adding them into a Set? Right now it's just adding everything.
I wanted to compare the objects by a String "name" for deciding the set membership.
 
Saloon Keeper
Posts: 11122
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to override the equals() and hashCode() methods.
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Fox wrote:I have a class that I made which is like a custom data-type, and I'm trying to figure out how to tell java what makes each object unique for the purposes of adding them into a Set? Right now it's just adding everything.
I wanted to compare the objects by a String "name" for deciding the set membership.



Check out the information at the below links. Hope it helps

Java Trail: Object as a Superclass

Deep Dive (with a video)

Why you must override hashCode if you override equals

Helpful Stack Overflow discussion

 
S Fox
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you both, I think I got it working. I wonder why it doesn't like having the @Override tag if I am indeed overriding a method? It tells me: "The method equals(MyClass) of type MyClass must override or implement a supertype method"
 
Carey Brown
Saloon Keeper
Posts: 11122
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because it should be
equals(Object)
 
Carey Brown
Saloon Keeper
Posts: 11122
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code for equals() and hashCode() can be tricky. Post your code here when you're done so we can check it.
 
S Fox
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here it is without an @Override tag, I cant add it to either one, the class just holds strings

 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Four things:
1) As said before, the single argument to the equals method should have Object as its type, not Sym. What you've done is overload the method, not override it.
2) You can merge your check for null and the instanceof check into one, because if the argument is null then instanceof will return false for every class.
3) Since this.symVal is an object (you can call hashCode() on it), you shouldn't use == to compare them. Use this.symVal.equals(other.symVal), or if they can be null use Objects.equals(this.symVal, other.symVal).
4) You use this.symProp in your hashCode implementation. This means that if two instances have equal values for symVal but different values for symProp, the instances are equal but have different hash codes. That's not allowed. You should either add an equality check for symProp in equals, or remove the use of symProp in hashCode.

So in short:
 
Carey Brown
Saloon Keeper
Posts: 11122
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about the trivial case where they refer to the same object?
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A pointless optimization most of the time.
 
S Fox
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is way more tricky than I first realized, I think I'm starting to understand it now.
I read somewhere it's bad to have the hash not be unique enough and adding more things together helps, that's why I added the other var.
How do we know if the hash is good enough, and what happens if it's a poor hash?
How come md5/sha1/sha256 hash are very unique but the java hash isn't?
 
Carey Brown
Saloon Keeper
Posts: 11122
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Fox wrote:I wanted to compare the objects by a String "name" for deciding the set membership.


You seemed to have made some design changes as this thread has progressed, I don't see "name" appearing in your equals() method. What are symVal and symProp?
 
Carey Brown
Saloon Keeper
Posts: 11122
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Fox wrote:I read somewhere it's bad to have the hash not be unique enough and adding more things together helps, that's why I added the other var.

You shouldn't add something just for the hash. Only add it if it is a meaningful part of a unique key for the object.

How do we know if the hash is good enough, and what happens if it's a poor hash?

Most implementations of hashCode() build on the hashCode() methods of other Java types, e.g. String. I trust them to be sufficient.

How come md5/sha1/sha256 hash are very unique but the java hash isn't?

These that you mention return a hash code longer than 32 bits. Java only deals with 32 bit hash codes. In addition, depending on the size of the hash table, is probably using a subset of those bits.
reply
    Bookmark Topic Watch Topic
  • New Topic