Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help sought in Collections ?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code i get the ouput as 3. my question is that how actually the elements are being added to the hashset. what i have understood(correct me if i'm wrong) is that when we did hs.add(h1) , internally the hashcode of h1 gets calcualted and then h1 is compared to other elements in the set. since at this point there are no elements in the set h1 gets added. when we did hs.add(h2) , again it calculated hashcode and then again it checks for equality using the overridden equals method. since the equal method now will be comparing h2's string with existing h1, which is of type propogate it will return false and h2 will be added. my question is that when we do hs.add(s1) , s1 is of type string does version of hashcode() and equals() method overridden by string class gets called or the one overridden by propagate gets called ? and whichever gets called can you please tell how the rest of statements viz hs.add(s1) and hs.add(s2) is accomplished ? also if while doing hs.add(s1) the equals() method overridden by string gets called what actually happens ? does s1 gets compared with h1, h2 (elements already in the set) ? collection is already taking toll on me. posted so many questions on this. i have been reading kb book. so can you guys also tell me which other book i can refer to ease my pain
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see java.util.HashSet#add(E)
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:


my question is that when we do hs.add(s1) , s1 is of type string does version of hashcode() and equals() method overridden by string class gets called or the one overridden by propagate gets called ?

the equals and hashcode methods of the object you are trying to add will get called.

whichever gets called can you please tell how the rest of statements viz hs.add(s1) and hs.add(s2) is accomplished ?

s1 will get compared to each of the objects already in the collection. String's equals method will return false when compared to a Propagate because String uses instanceof String as part of its equals check.
s2 will get compared to each of the objects already in the collection. It will find a match with s1.

also if while doing hs.add(s1) the equals() method overridden by string gets called what actually happens ?

String's equals method first checks == for identity (is it the same object), then uses instanceof String to weed out anything that is not a string.

does s1 gets compared with h1, h2 (elements already in the set) ?

Yes.

Check this out:


even when everything is "1", there will still be three members of the Set because s1 will not be equal to any Propagate objects but s2 will equal s1

but if you change the order of things added to hs, it will only have one member because Propagate uses itself.str to compare so everything except the first string added will not be allowed into the set:


in other words, propagate.equals(aString) can return true, but aString.equals(aPropagate) can never return true
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot Tim. That was really an excellent explanation and also very nice way of making things understand. thanks man
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You bet. I appreciate the link A LOT.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic