aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help sought in Collections ? 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 "help sought in Collections ? " Watch "help sought in Collections ? " New topic
Author

help sought in Collections ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1



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

Joined: Jan 28, 2008
Posts: 5575

see java.util.HashSet#add(E)
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

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

Joined: Apr 04, 2012
Posts: 924
    
    1

thanks alot Tim. That was really an excellent explanation and also very nice way of making things understand. thanks man
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

You bet. I appreciate the link A LOT.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help sought in Collections ?