hashcode is calculated on the basis of value object

Hi,

I am reading below link
http://www.java2blog.com/2013/02/difference-between-hashmap-and-hashset.html

I have not understood below point

In this,hashcode is calculated on the basis of value object.Hashcode can be same for two value object so we have to implement equals() method.If equals() method return false then two objects are different.
Read more at http://www.java2blog.com/2013/02/difference-between-hashmap-and-hashset.html#ZZEA3viEKQ7T9uHh.99



what it mean by hashcode is calculated on the basis of value object.

Based on below example
http://www.java2blog.com/2014/07/how-hashset-works-in-java.html

I understood that india printed only once as we have not implemented hashCode and due to that hashCode different for india1 and india2 object so wont bother to check equals.

But original point (hashcode is calculated on the basis of value object) seem reverse of above concept. please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
He means the hashcode is taken from the actual object. Since there is no key in a HashSet data structure.
0
Radek BaranowskiFull-stack Java DeveloperCommented:
I understood that india printed only once as we have not implemented hashCode and due to that hashCode different for india1 and india2 object so wont bother to check equals.

It printed only once BECAUSE it was after implementation of HashCode.

In the first case execution used native hashCode method, as explained:
Now you must be wondering even through two objects are equal why HashSet contains two values instead of one.This is because First HashSet calculates hashcode for that key object, if hashcodes are same then only it checks for equals method and because hashcode for above two country objects uses default hashcode method,Both will have different memory address hence different hashcode.
Basically, the hashset generated two different keys for those two ojects so the size of the set was 2

Now, after hashCode method has been implemented in the way that provided identical hashCodes for two different Country object which had the same name inside, the key generated upon adding country to the hashset was the same for both objects, so the resulting size of the set was 1.

 result = prime * result + ((name == null) ? 0 : name.hashCode());

Open in new window


Above will generate the same hashCode for two object with "India" inside, as "india" is String and will result in equals == true when using Java-builtin equals for Strings.

Hope this is clear enough :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.