There is this class called Testcoll which has main method . the main method creates 3 objects of type Hashable and adds each object to collection one by one . After adding first object . This code edits the parameter on which equals and Hashcode depends in first object . and again adds this object to hashset. now this first object is contained in 2 hash buckets with same value .
This was contradictory to my knowledge . the hashset shouldn't contain duplicate values and should not contain same object twice . I also remember studied that the value on which hashvalue depends should not be change during the entire run of program. but can be changed during next run.
So this implied to me that data in collections which uses hashcode, Should be handled carefully and data in objects should not be changed without our knowledge that data would have on hashvalue of the object . Is this how collections is suppose to be used . Or what ?? Please could Someone give me answers . Its humiliating for me that i am not completely aware of collection framework .
It also implied to me that Hashset is not self adjusting collection according to the hashvalue . the arrangement of objects is done only at time of their insertion . Is this true
below is the code
Harsha kumar Reddy wrote: After adding first object . This code edits the parameter on which equals and Hashcode depends in first object . and again adds this object to hashset. now this first object is contained in 2 hash buckets with same value .
When you put an object in a hash-based collection (a collection such as HashSet, or as a key in a HashMap) and you change the object so that its hash code changes, then you will get strange and unpredictable effects.
The object will suddenly in the wrong bucket in the collection and the hash-based collection will get confused, so it might happen that it can't find the object anymore even though you put it in the collection.
Objects which you put in a hash-based collection must never be modifiable in such a way that the hash code might change.
Note that a HashSet cannot automatically adjust for this, because the HashSet does not know that you change the object after you've put it in the HashSet. It indeed only looks at the hash code of the object at the moment you insert it in the HashSet. When you change the object afterwards, the HashSet will get confused.
It is a bit surprising that neither the Java® Tutorials nor the API documentation for HashMap and HashSet tell you that you mustn't change the hash code of their elements or Keys. Both the Map interface and Set interface, however, tell you
and you will find similar warnings about the equals() method in Odersky Spoon and Venners.
Note: Great care must be exercised if mutable objects are used as …
I changed the red writing in your post to black because many people find coloured text difficult to read.
Campbell is absolutely right ..Don't change the key's value when you are putting that key in the hash based collection .. That is the reason we generally choose immutable object as a key in this scenario .
IMMUTABILITY allows you to get same hash code every time, for a key object.