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

hashCode() and equals()

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Few Questions :

1. Does String and enums overload both hashCode() and equals() ?

2. If we want to add object to a collection whose name start with Hash(eq. HashSet,LinkedHashSet,HashMap,LinkedHashMap,Hashtable) the object must override hashCode() and equals(). Otherwise what will happen? The Object Class's hashCode() will put each entry in a seperate bucket or something else ?

3. Can we use any collection as generic and non-generic and non-generic means it's elements must be Object(or it's any subclass).
I mean
Is both OK?
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
Both are ok.But you are defining first one with no type safety otherone with type safety.Strings and enums uses proper overring techniques. While testing the Hashnamed collections(eg. HashSet<String> String version is recommended.
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Can anybody provide answer for all 3 questions ?
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
The three are answared know,So I think I have to Give like this
1. Does String and enums overload both hashCode() and equals() ?
Ans:__They overload hashCode() and equals()

2. If we want to add object to a collection whose name start with Hash(eq. HashSet,LinkedHashSet,HashMap,LinkedHashMap,Hashtable) the object must override hashCode() and equals(). Otherwise what will happen? The Object Class's hashCode() will put each entry in a seperate bucket or something else ?
Ans:__If we donot override means,Objects version will be called(because any class is a subclass of Object).So the objects which given with a meaningfully equal(This is upto the programmer to decide)manner will also be seated in different buckets.So,we cannot access them while searching.

3. Can we use any collection as generic and non-generic and non-generic means it's elements must be Object(or it's any subclass).
I mean
code:
Set s = new HashSet();
Set <String> s1 = new HashSet<String>();
Is both OK?
Ans:__You are write in first case all the subclasses of Object are elements.
In the second case only String object be the elements
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers!

Point 1:

Yes, String overRIDES both equals and hashCode.
Also class Enum<E> overrides both methods, see API.



Point 2:
for equals: if not overridden, equals will return true, if the reference points to the same object. And returns false, if the references point to distinct objects, regardless if you and me would treat the objects as different or equal.
You can add as many objects as you like into a Set, only their references must be unequal.
In the example below, sammy_II is added into the Set, but it shouldn't, because it has the same name as sammy. But equals is not overridden.

for hashCode: as you have written: Every entry in a separate bucket.
So while billy_II should be the same Horse as billy, it cannot be found in the Set.


prints :
Size: 3
is Billy II. in the corral?
no




Point 3:

EXAM DANGER

there are questions on the exam where you have to discern between:
compiles without warning and
compiles with warning

eg
Set s = new HashSet(); //1
Set<String> s1 = new HashSet<String>(); //2
s.add("Hello"); //3
s1.add("Hello"); //4
String str=s1.get(0); //5




3 compiles with, 4 without warning.
5 does not compile at all ("set - no get").


Yours,
Bu.
[ October 29, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode() and equals()