• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with adding elements to HashSet  RSS feed

 
Harsha kumar Reddy
Greenhorn
Posts: 12
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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
Note: Great care must be exercised if mutable objects are used as …
and you will find similar warnings about the equals() method in Odersky Spoon and Venners.

I changed the red writing in your post to black because many people find coloured text difficult to read.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harsha,

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.

Thanks ,
Satya
 
Harsha kumar Reddy
Greenhorn
Posts: 12
Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it is like how i interpreted . The value on which hashcode and equals depends should not be changed after adding object to collections .
Thanks Campbell and Jesper for clearing my doubt on this topic .And proving some useful reference links
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!