krelman
asked on
hasCode for 3 int.
Hi,
I need your advise.
I have 3 integer a,b,c in myClass
and I want to insert this Class has key in HashMap, then I need to Implment HashCode for this case
What is the best why to do this ?
I thought about
String s = a + "#" + b + "#" + c;
Ami
Thanks.
I need your advise.
I have 3 integer a,b,c in myClass
and I want to insert this Class has key in HashMap, then I need to Implment HashCode for this case
What is the best why to do this ?
I thought about
String s = a + "#" + b + "#" + c;
Ami
Thanks.
Typically xor is used: a ^ b ^ c
hashCode() you could implement as
return a * b * c;
return a * b * c;
So,
public int hashCode() {
return a ^ b ^ c;
}
public int hashCode() {
return a ^ b ^ c;
}
Keep in mind that if you're overriding hashcode like this, you might also want to override .equals(). Two objects that .equals() eachother should have the same hashcode.
Definitely true.
(I supposed that as known by the author)
(I supposed that as known by the author)
public int hashCode()
{
return a + b + c;
}
public boolean equals(Object o)
{
myClass mc = (myClass) o;
return a==mc.a && b==mc.b && c==mc.b;
}
{
return a + b + c;
}
public boolean equals(Object o)
{
myClass mc = (myClass) o;
return a==mc.a && b==mc.b && c==mc.b;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> I need your advise
Hi krelman, what do you think of our advices so far?
Hi krelman, what do you think of our advices so far?
ASKER
Hi,
has you write all the previous solution don't have a uniq result.
Are you sure this have a uniq result ? ( I am checking it )
public int hashCode()
{
return a + (b<<1) + (c<<2);
}
has you write all the previous solution don't have a uniq result.
Are you sure this have a uniq result ? ( I am checking it )
public int hashCode()
{
return a + (b<<1) + (c<<2);
}
ASKER
Hi,
has you right - all the previous solution don't have a uniq result.
Are you sure this have a uniq result ? ( I am checking it )
public int hashCode()
{
return a + (b<<1) + (c<<2);
}
has you right - all the previous solution don't have a uniq result.
Are you sure this have a uniq result ? ( I am checking it )
public int hashCode()
{
return a + (b<<1) + (c<<2);
}
>>Are you sure this have a uniq result ?
No I'm not. I guess it isn't.
But it shouldn't be unique, it should be "as unique as possible".
If it isn't unique that is *no functional problem*: the item will be found.
No problem.
But it *could* be a performance problem: for one (nearly unique) key, multiple items are found.
And this leads to some extra comparisons.
No I'm not. I guess it isn't.
But it shouldn't be unique, it should be "as unique as possible".
If it isn't unique that is *no functional problem*: the item will be found.
No problem.
But it *could* be a performance problem: for one (nearly unique) key, multiple items are found.
And this leads to some extra comparisons.
>> I am checking it
Don't look any further
a=0, b=0, c=4
a=16, b=0, c=0
a=0, b=8, c=0
all produce 16 as hash code. (No "real" problem as said before)
But for a,b,c values different of 0, it looks rather difficult to find two sets that produce the same code.
So, I guess you're rather safe with that.
Don't look any further
a=0, b=0, c=4
a=16, b=0, c=0
a=0, b=8, c=0
all produce 16 as hash code. (No "real" problem as said before)
But for a,b,c values different of 0, it looks rather difficult to find two sets that produce the same code.
So, I guess you're rather safe with that.
I haven't done any statistical research on this, but i'd guess that the introduction of primes could be of use, but don't get too hung up on this. As has already been said, complete uniqueness is not required
To cope with the 0 problem you could go further and use
public int hashCode() {
return a + ((b+1)<<1) + ((c+1)<<2);
}
or
public int hashCode() {
return ((a+1)<<1) + ((b+1)<<2) + ((c+1)<<3);
}
The sky is the limit.
But you have to ask yourself if the (slightly) better performance is worth the (looking for the holy grail-) effort.
public int hashCode() {
return a + ((b+1)<<1) + ((c+1)<<2);
}
or
public int hashCode() {
return ((a+1)<<1) + ((b+1)<<2) + ((c+1)<<3);
}
The sky is the limit.
But you have to ask yourself if the (slightly) better performance is worth the (looking for the holy grail-) effort.
Thanks for accepting.
That keeps us answering your future questions too.
That keeps us answering your future questions too.