Solved

hasCode for 3 int.

Posted on 2004-04-19
15
236 Views
Last Modified: 2008-03-10
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.


 
0
Comment
Question by:krelman
  • 9
  • 2
  • 2
  • +2
15 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10859225
Typically xor is used:  a ^ b ^ c
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10859229
hashCode() you could implement as

return a * b * c;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10859247
So,

public int hashCode() {
   return a ^ b ^ c;
}
0
 
LVL 1

Expert Comment

by:derkec
ID: 10860517
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10860588
Definitely true.
(I supposed that as known by the author)
0
 
LVL 92

Expert Comment

by:objects
ID: 10864592
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;
}
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 10869540
This one is even better:

public int hashCode() {
   return a + (b<<1) + (c<<2);
}

Why?
The goal of the hashCode is to construct a key for the HashMap.
The more unique the key is, the better. (=the faster the lookup will be)

All previous (valid) solutions (^, * or +) give the same result for the following
three different objects:

            a=1, b=2, c=4
       
            a=4, b=2, c=1
and
            a=1, b=4, c=2

while the above produces three different hash codes.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:zzynx
ID: 10869555
>> I need your advise
Hi krelman, what do you think of our advices so far?
0
 

Author Comment

by:krelman
ID: 10869625
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);
}
0
 

Author Comment

by:krelman
ID: 10869631
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);
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10869693
>>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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10869770
>> 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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10869822
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10869859
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10875566
Thanks for accepting.
That keeps us answering your future questions too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 126
backtracking recursion  code 19 54
HSSFWorkbook cannot be resolved error 10 50
servlet example issue 6 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now