• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 41
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

Hope it helps
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you...................
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK............
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic