aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is it alright to use the string member's hashcode as hashcode for the Object ? 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 "is it alright to use the string member Watch "is it alright to use the string member New topic
Author

is it alright to use the string member's hashcode as hashcode for the Object ?

tapeshwar sharma
Ranch Hand

Joined: Mar 10, 2006
Posts: 245
Hi,
I have an object X that has a String unique key member, amongst other instance member variables.
Since String has a unique and consistent hashCode output, is it alright if I use the hashcode value of the String unique key member as the hashcode for X ?
Kidly look at the mock class below to understand my question :
class X {
String a; //unique
int b;
.....
.....

public int hashCode(){
return a.hashCode();
}
public boolean equals(X x){
.......
.......
}
}
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Prashant,

Go with that, there is no harm of using hashCode of the string "different string will give different hashCodes".


Thanks and Regards,
cmbhatt


cmbhatt
Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
It is ok to use the String instance member's hashcode as the hashcode of the
class X. Your purpose is to store X objects as keys of Hashmap, right. Instead
storing X objects as keys, you can store those String instance members as keys, right
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi prashant,

Its different to say is it compilable? is it the correct convention?

1) Is it compilable ? -- YES
2) Is it correct convention ? NO

hashCode() value should change when "object state" changes(some may collide).

If you depend up on only one instance variable out of many instance variables, its in-efficient(though it works orcompiles).

Hope its clear.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3205
    
    2
It depends on how your equals method is coded.

For example, your hashcode method would be invalid if your equals is :

public boolean equals(X x){
...
return this.b == x.b;
}

but it would be valid if the equals is:

public boolean equals(X x){
return this.a == x.a;
}

Remember that the rule is if equals() returns true, then hashCode must return the same value for both the objects. So you cannot comment on the validity of equals() or hashCode() without looking at the other.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Srini,


On what ground you say

2) Is it correct convention ? NO


So far as String is concerned, it is OK to go with hashCode() of the String.
Please correct me if I go missing something.


Thanks and Regards,
cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,


As Paul Anil pointed out,

How we define equals method is very important. Lets come in that angle.

We generally take into consideration Full Object state when we define equals for a class.

So efficeint way is to have different hashCode for two distinct Objects.
So be consistantly efficient with equals hashCode must take full object state. Otherwise it will produce more collisions. or clustering behaviour. or more elements placed in one Bucket.

Hope its clear.
tapeshwar sharma
Ranch Hand

Joined: Mar 10, 2006
Posts: 245
Srinivasan,
quote:
____________________________________________________________________________
hashCode() value should change when "object state" changes(some may collide).
_____________________________________________________________________________
unquote

Can you please elaborate on that ?
vis-a-vis equals method, I am aware of the contract,so that's okay.
Thanks for your post, by the way.
tapeshwar sharma
Ranch Hand

Joined: Mar 10, 2006
Posts: 245
Srinivasan,
Sorry, just upon clicking on the "Add Reply" button, did I realise
what a stupid question I am asking.Kindly ignore my earlier post.
So, if I use other instance variables like the ones I have of type int etc.,
will that be okay ?
regards,
Prashant
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Prasant,

It depends how you consider, your class objects equal.
What and all involved in equals should be considered for hashCode which generates distinct codes almost all the time when Object state changes.

Have a nice time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it alright to use the string member's hashcode as hashcode for the Object ?
 
Similar Threads
equals and hashCode
Help on Hashcode
Problem with HashMap
can you please this doubt about hashcode()
Equals e HashCode