• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
tapeshwar sharma
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ganesha Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3715
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic