Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashSet and Strings doubt

 
Rafael Angarita
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Daniel Faria
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You have to check equal and == .

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

Include following line in your code and check.




HTH
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rafael Angarita
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rafael Angarita
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,

Problem solved!

Cheers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic