File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes adding objects with same hashcodes and true value return by equals() method to a set. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "adding objects with same hashcodes and true value return by equals() method to a set." Watch "adding objects with same hashcodes and true value return by equals() method to a set." New topic
Author

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

kamal krishna bhatt
Ranch Hand

Joined: Apr 26, 2011
Posts: 41


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

Joined: Mar 30, 2005
Posts: 225



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

Hope it helps


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

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

Joined: Apr 26, 2011
Posts: 41

thank you...................
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3013
    
  10
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

Joined: Apr 26, 2011
Posts: 41

OK............
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding objects with same hashcodes and true value return by equals() method to a set.