wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashSet and Strings doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashSet and Strings doubt" Watch "HashSet and Strings doubt" New topic
Author

HashSet and Strings doubt

Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
Hello guys,

I'm doing some experiments to practice for my test. I don't understand what's going on with the next piece of code:



Output:
true
true
true
false
3
false

I don't know why the two StringBuilder objects are being added to the HashSet while only one String object is being added.

Thank you guys,


Rafael Angarita.
SCJP 6.
Daniel Faria
Greenhorn

Joined: Dec 25, 2007
Posts: 15
Are you from Brazil as me??

The class HashSet use the method equals for know if exist another copy of object in HashSet.
If you wanna know s1 and s2 can put should use method equals return false.
Sorry for my enghish is no very good.
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
Hi,
You have to check equal and == .

== : Compare references not values.
equals : Compare values.

Include following line in your code and check.




HTH


SCBCD, SCJP & MCP
HowToAskQuestions
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
String overrides equals() and implement to confirm meaningful equivalence between objects with differences. Here you are trying to add two meaningfully equivalent String objects in a Set, and a Set doesn't allow duplicate elements.

String Builder does not override equals() and hence it only checks for reference equivalence.
You can change the reference on line9 as

StringBuilder b2 = b1;

Set will no longer take it in.


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
Thank you so much guys. I think I got that.

Now, I've changed the code a little bit:



Now I'm overriding the equals method (of course, I know, it's a stupid overriding) and I'm inserting two HashSetTest objects that are supposed to be meaningful equivalents since equals always returns true.

I'm still getting the same output. It seems the equals method of HashSetTest is not being called.

What am I missing here?

(Daniel, I'm your neighbor from Venezuela )
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

Now I'm overriding the equals method (of course, I know, it's a stupid overriding) and I'm inserting two HashSetTest objects that are supposed to be meaningful equivalents since equals always returns true.

I'm still getting the same output. It seems the equals method of HashSetTest is not being called.

What am I missing here?


HashSet (actually, all the hashing collections) use the equal() and hashCode() method to hash and store objects -- you need to override both methods. And you need to override it in a way that won't break the equals/hashcode contract. See the JavaDoc for the Object class for details on the contract details.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
Thank you,

Problem solved!

Cheers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashSet and Strings doubt
 
Similar Threads
Employee with same values is being added two times in HashSet
about String & StringBuffer
Question on Strings and String pool
HashSet question
Doubt in function remove in Set