Stephan van Hulst wrote:What does it matter? Isn't it enough that HashSet's internal consistency depends on the correct implementation of equals() and hashCode(), regardless of what method you call?
Stephan van Hulst wrote:You're putting the cart before the horse.
"If you don't override equals() and hashCode() you will NOT be able to add multiple references to the same object to a Set."
if one uses HashSet but doesn't override equals and hashcode methods , then there would be no point in using a Set (which by its nature doesn't allow duplicates) ,when duplicates would not even be a possibility in the first place (as equals and hashcode are not overridden)
A Hashbucket is like address of object in memory.
Monica Shiralkar wrote:I have a question. In which scenario does the case arises that two objects have the same hashcode but are not equal?
No. Hashing is a way to reduce an object to a numberMonica Shiralkar wrote:Overall, below is my understanding on hashing , hashcode and equals methods.
1. Hashing is the concept used by some collections like HashSet,HashMap and HashTable for faster retrieval performance.
That sounds all confused, as if you didn't know the difference between a memory location, a pointer, and a reference. The bucket indirectly contain references to the objects in question, which might be anywhere in heap memory.2. Instead of having the elements scattered all over the memory, if those are stored in the hashbuckets based on hashcode, then the retrieval would be fast. A Hashbucket is like address of object in memory.
That is a platform specific implementation detail. C has structs rather than what we call objects, and they don't return hash codes by default. You would have to create a function to calculate a hash code.3. Each object has a hashcode value and by default each object is unique.
“implement”? Surely you meant, “override”?. . . 5. For using the objects as elements in HashSet or as keys of HashMap, the class should implement the hashcode and equals method. The latter wiill have the conditions based on which objects would be considered duplicates.
You mean they must have the same hash code. Not “similar”.. . . 7. If two objects are equal . . . those should have similiar hashcode. . . .
Campbell Ritchie wrote:
No. Hashing is a way to reduce an object to a numberMonica Shiralkar wrote:Overall, below is my understanding on hashing , hashcode and equals methods.
1. Hashing is the concept used by some collections like HashSet,HashMap and HashTable for faster retrieval performance.That sounds all confused, as if you didn't know the difference between a memory location, a pointer, and a reference. The bucket indirectly contain references to the objects in question, which might be anywhere in heap memory.2. Instead of having the elements scattered all over the memory, if those are stored in the hashbuckets based on hashcode, then the retrieval would be fast. A Hashbucket is like address of object in memory.
That is a platform specific implementation detail. C has structs rather than what we call objects, and they don't return hash codes by default. You would have to create a function to calculate a hash code.3. Each object has a hashcode value and by default each object is unique.
What is meant here by "unique"?“implement”? Surely you meant, “override”?. . . 5. For using the objects as elements in HashSet or as keys of HashMap, the class should implement the hashcode and equals method. The latter wiill have the conditions based on which objects would be considered duplicates.
You have already been told that the equals() method defines which two instances are and are not equal to each other. Yes, you can use objects with un‑overridden equals() and hashCode() in hash‑based data structures, and I think you have already been told how such data structures will behave.You mean they must have the same hash code. Not “similar”.. . . 7. If two objects are equal . . . those should have similiar hashcode. . . .
Thank youJesse Silverman wrote:. . . Happy New Year everyone!
That is what everybody thinks, but it isn't necessarily true. A memory location can change, for example whenever garbage collection happens, and the current API documentation doesn't say anything about memory locations. Some old versions did.. . . . without overriding . . . .hashCode() . . . each instance of an Class will return a unique specific value based solely on its address in memory . . .
Can't arrive too soon.Project Valhalla . . .
That's a pleasureCampbell: Thanks so much . . .
That is because there isn't such a distinction in Java®. After Valhalla, that might be a different story.distinctions between value types and reference types . . . .
Campbell Ritchie wrote: as if you didn't know the difference between a memory location, a pointer, and a reference
Campbell Ritchie wrote:The bucket indirectly contain references to the objects in question, which might be anywhere in heap memory.
Paul Clapham wrote: But there's only 2^32 possible hash codes.
Carey Brown wrote:for example, a HashMap with only 64 buckets.
Carey Brown wrote:unless you "put" it there. So, only a select few of the references to all the objects created by a running program end up in hash buckets.
Carey Brown wrote:When you create a HashMap instance, for example, that instance owns its own set of buckets. If you create another HashMap that has its own buckets. The two sets are completely different. The reference to a particular data object may be "put" in either one, or both.
Campbell Ritchie wrote:I would prefer the placeholder code you get with NetBeans:-
Carey Brown wrote: I guarantee you'll understand it a whole lot better when you're done.
Campbell Ritchie wrote:
No. Hashing is a way to reduce an object to a numberMonica Shiralkar wrote:Overall, below is my understanding on hashing , hashcode and equals methods.
1. Hashing is the concept used by some collections like HashSet,HashMap and HashTable for faster retrieval performance.
Campbell Ritchie wrote:The hash data structure doesn't do any hashing, at least not in Java® (it might in other languages). In Java® each object does its own hashing.
That's a pleasureMonica Shiralkar wrote:. . . Thanks.
Spot on. . . Every object has a hashcode . . . hashing is done . . . by the objects themselves . . . .
The moustache of a titan! The ad of a flea:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
|