aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion about which collection framework has the responsibility of hashcode() and.. 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 "Confusion about which collection framework has the responsibility of hashcode() and.." Watch "Confusion about which collection framework has the responsibility of hashcode() and.." New topic
Author

Confusion about which collection framework has the responsibility of hashcode() and..

Wilson Mui
Ranch Hand

Joined: Apr 09, 2003
Posts: 140
I am a little confused, lets say I have a statement like this:

The answer to this question was 3, because it said that the class Computer did not implement the hashcode() method, and therefore would never call the equals() method (which was implemented in the class Computer). But I always thought it was the collection type that comparison. I thought it was up to the Set class, in this case, to do the comparing, however it decides to compare the objects. Because otherwise, it leaves the responsibility of the class creator to implement the hashcode() method as well as the equals() method, and god knows you can't trust people. And what will happen is, you'll have a set thinking it has no duplicated members and you'll be wrong...and that can be a catastrophe. So is it a catastrophe or am I just confused?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If hashCode has not been overridden, then presumably equals has also not been overridden.
The equals method inherited from Object returns false if object references are not the same. As there are three different Computer objects, then they are all added to the LinkedHashSet.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Wilson Mui
Ranch Hand

Joined: Apr 09, 2003
Posts: 140
well I wouldn't assume that not implementing (overriding) the hashcode() method usually means not implementing (overriding) the equals method. I think a lot of people, myself included often override the equals method but forget about the hashcode() method. Anyway, it sounds like you are saying, unless a specific class overrides both the hashcode() and equals() method, the collection framework interfaces will not "properly" store the elements. Meaning the way you, "probably" want it sorted. P.P.S Meaning even if you store the Objects of Class X in a set, that doesn't guarantee they will only have 1 recurrence in the set as long as they are different references.
Is that true...and confusing enough?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The key point here is that the inherited Object's equals method is the same as the == operator - very discriminating! So, two objects with the same reference cannot both be added to a set, thus ensuring the contract that a set must contain unique elements. What happens is that the first is added but all subsequent objects with the same reference are rejected.
Wilson Mui
Ranch Hand

Joined: Apr 09, 2003
Posts: 140
Ok, so basically the Set interface's unique object intelligence is all based on the fact that it just implements it's equals() method with a == and therefore only allows reference unique objects?
You mean the Set interface will allow two Integer(5) objects, meaning
Integer Int1 = new Integer(5);
Integer Int2 = new Integer(5);
Set will allow both of them in it's Set. If that is the case, then Set's unique object bonus, really isn't much help I don't think.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Wilson, the API says that Integer.equals(Object) overrides Object.equals(Object) properly. That is, comparing their int values.


SCJP2. Please Indent your code using UBB Code
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Wilson Mui:
Ok, so basically the Set interface's unique object intelligence is all based on the fact that it just implements it's equals() method with a == and therefore only allows reference unique objects?
You are missing the point. The Set doesn't use it's own implementation of the equals method to determine equlaity. It uses the equals method of the class it is adding to the Set. If I am adding two Integer objects to a Set then it depends on how the programmer implemented the equals method in the Integer class not anything in the Set class's equals method. All the Set does is run the equals method of the object that is being added to the Set.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion about which collection framework has the responsibility of hashcode() and..
 
Similar Threads
Question regarding javacertificate.com study question
Collection(regarding order elements)
K&B - Erratum ?
confusion about comparator, comparable and hashcode
Using java.util.HashSet for Custom Class