Javier Gonzalez wrote:Hi Campbell! So the contract will break because we use more variable fields in the hashCode() than in the equals()? but not when we use both in equals() and just one in hashCode()?
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
Tim Holloway wrote:
Ideally, no two objects won't have the same hashCode, since that can make hash-based searches to be less efficient.
Ideally, no two objects won't have the same hashCode, since that can make hash-based searches to be less efficient.
. . . but that ideal is often impossible to achieve, especially if the range of different kinds of object from one class has a cardinality > 2³².Tim Holloway wrote:. . . Ideally, no two objects won't have the same hashCode . . .
Jesse Silverman wrote:
In standard English, we hope no two objects will have the same hashCode(), yes?
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
Monica Shiralkar wrote:Are the below correct?
1) No 2 objects would have the same hashcode (unless, not only do we override hashcode and equals but also hard code the hashcode , which is a bad practice).
2) If we override the hashcode and equals, then it will not have same hashcode but one thing is for sure that these objects will land in the same bucket.
Jesse Silverman wrote:2) Also no, distinct objects will rarely have identical .hashCode() values, but it will eventually happen, sooner if we don't pick a good .hashCode() implementation for the class.
Jesse Silverman wrote:I think I understand what some of the programmers who are OCD about .hashCode() implementations are saying right now, in terms of where they get literal bits of difference.
They are talking about how often you will have two different .hashCode() values but wind up in the same bucket anyway. You want to make sure the lower order bits have a good distribution to minimize this.
Incorrect. We have already said it is desrirable for two unequal objects to have different hash codes, but that cannot always be achieved.Monica Shiralkar wrote:Are the below correct?
1) No 2 objects would have the same hashcode (unless, not only do we override hashcode and equals but also hard code the hashcode , which is a bad practice).
Not sure I understand that, but it feels incorrect, too.2) If we override the hashcode and equals, then it will not have same hashcode but one thing is for sure that these objects will land in the same bucket.
Too tired to go into the grammar, but such double negatives aren't quite exact equivalents to the affirmative.Jesse Silverman wrote:. . . Like "not indistinct" = "distinct". . . .
No. The constructors don't mention equals() nor hashCode(). Similarly for HashSet, I would have thought those constructors are easy enough to learn, and don't know whether they are in the exam syllabus.. . . Would you agree that understanding the overloaded constructors for HashMap and HashSet are probably the next level up beyond understanding the .hashCode()/.equals()/.compareTo()/.compare() contract? . . .
The documentation for TreeSet specifically tightens the general contract for Set by insisting the comparison technique be “consistent with equals,” which meansSo you are using compare() (or compareTo()) as a surrogate for equals().. . . a TreeSet or TreeMap could violate non-duplicate rules in terms of .equals() . . . the Set no-duplicates rule is in terms of .equals().
Isn't there just.. . . there's a lot of low-quality material out there . . .
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
Campbell Ritchie wrote:Incorrect. We have already said it is desrirable for two unequal objects to have different hash codes, but that cannot always be achieved
Not sure I understand that, but it feels incorrect, too.
Monica Shiralkar wrote:But how come it being 'desirable for two unequal objects to have different hash codes but not always being achieved' means that the below statement of mine is incorrect?
"No 2 objects would have the same hashcode (unless, not only do we override hashcode and equals but also hard code the hashcode , which is a bad practice"
I meant that if we override the hashcode and the equals method in a class, then all objects of this class will be in the same hash bucket if stored in hashset/hashmap?
Of course not. What would be the point if all objects ended up in the same bucket? Objects with different hash codes are preferably distributed uniformly over different buckets.
Objects with the same hash code will always end in the same bucket. So do your best to give different objects different hash codes.
Stephan van Hulst wrote:
You say that no two objects would have the same hash code. That's impossible because there is an almost infinite number of distinct objects, but a limited number of distinct hash codes.
.
Stephan van Hulst wrote:Objects with the same hash code end up in the same bucket. Other than that, there are no guarantees.
Jesse Silverman wrote:Hi Monica:
which would be legally satisfied by hard-coding the same value for everything to be returned by your .hashCode().
If you did that, you could never get a "broken" structure accidentally containing duplicates, but you would get very poor performance because every item would get stuffed into the same bucket, losing the super-fast lookup times to find things.
Monica Shiralkar wrote:
Yes, but if "Objects with the same hash code end up in the same bucket" then why would they be in different bucket in case of same hard coded hashcode?
Jesse Silverman wrote: if you hard-coded a constant value for hashCode(), every single entry would always end up in the exact same bucket all the time.
Stephan van Hulst wrote:
If each object returned a unique (== distinct) number, they would be distributed over different buckets fairly uniformly
Monica Shiralkar wrote:
Understood that what I said was wrong as hashcode is not a unique number.
And had that been a unique number all objects of this class would have landed in the same bucket.
If in a class hashcode is overridden and hard coded and also the equals method is overridden, all objects of this class would have landed in the same bucket.
Stephan van Hulst wrote:Yes. In such a case, the map or set essentially turns into a linked list.
Stephan van Hulst wrote:Yes. In such a case, the map or set essentially turns into a linked list.
Not 7+; either 8+ or 9+.Jesse Silverman wrote:. . . (since version....7 or 8?) . . .
No, you have a load factor and once the load factor is exceeded, the structure is enlarged with more buckets. So in theory the number of entries per bucket is reduced. The arithmetic or bit‑twiddling needed to find the bucket number is unchanged and should execute at the same speed. Remember arrays support random access.. . . very large hash structures
Yes, that is the reason. The documentation for HashMap doesn't say anything about trees; the following is the nearest it gets to it:-. . . less-than-ideal hashCode() implementations . . .
That sentence appears in the Java8 version and not in the Java7 version, suggesting it was in Java8 that they changed to a tree. And that change may only apply when the keys implement Comparable. You would have to find the source code to be certain about that.. . . when keys are Comparable, this class may use comparison order among keys to help break ties. . . .
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |