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.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
S Fox wrote:I wanted to compare the objects by a String "name" for deciding the set membership.
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.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.
Most implementations of hashCode() build on the hashCode() methods of other Java types, e.g. String. I trust them to be sufficient.How do we know if the hash is good enough, and what happens if it's a poor hash?
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.How come md5/sha1/sha256 hash are very unique but the java hash isn't?