Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashset

 
gunjan khanuja
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



i run this code snippet and this gives me output 3
I understand that hashset contains unique elements so the string s1 and s2 is not added twice but why is Propagate object being added twice..???explain
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gunjan khanuja,

Welcome to CodeRanch!

gunjan khanuja wrote:why is Propagate object being added twice..???explain

Well, I would have liked a 'please' here

Anyways, here's a hint : take a close look at your equals method. What exactly are you trying to equate?
 
gunjan khanuja
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,I am trying to compare the string value with object 's value.But i dont understand the output.
please explain in detail what all is happening in the snippet???
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar's pointed you in the right direction. In your equals() method, you are comparing a String with a Propagate.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still not clear? then you customize your equals method as in
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint : try to System.out.println(h1.str.equals(h2)). Is the answer true or false? I think it is false.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic