I am OCPJP SE 6 . I stumbled upon an issue which i couldn't answer to my junior regarding collections .Could Please someone explain what's happening behind this code
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