• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding objects with same hashcodes and true value return by equals() method to a set.

 
kamal krishna bhatt
Ranch Hand
Posts: 41
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when equals() returns false then output :
[abc@1, abc@1, abc@1, abc@1] no problem with that.

but when i run above code gives output:
abc@1
which means it is not going to add any duplicate object to set as a set checks for hashcode first then equals method to test two objects equality.
but does it really adds one object or it makes a linked list out of these all objects.

 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


check the return type which says whether it has added or not

Hope it helps
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

A Set doesn't do anything with linked lists. When you put in four different objects that have the same hash code and for which equals() return true, then it will just store the last object that you put in, and it forgets about the previous objects. So no, it does not make a linked list out of the objects; it will just forget about the objects that you put in before.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch.

A Set doesn't do anything with linked lists. When you put in four different objects that have the same hash code and for which equals() return true, then it will just store the last object that you put in, and it forgets about the previous objects. So no, it does not make a linked list out of the objects; it will just forget about the objects that you put in before.


as said by Jong , it have recent object since it maintains Map
 
kamal krishna bhatt
Ranch Hand
Posts: 41
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you...................
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:A Set doesn't do anything with linked lists.

Well, a HashSet does use linked lists internally, to handle different objects that end up in the same hash bucket. Which is what is happening when you have equals() return false - the objects have the same hash code, so they end up in the same bucket, but since according to equals() they are all different objects, they all get separate entries in the HashSet, stored in a single bucket using a singly-linked list. When you change the code to make equals() return true, the linked list is unnecessary, because the objects all appear to be the same object. But the linked list is still part of the way HashSet is implemented internally.
 
kamal krishna bhatt
Ranch Hand
Posts: 41
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK............
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic