Solved

hasCode for 3 int.

Posted on 2004-04-19
15
223 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
Comment Utility
Typically xor is used:  a ^ b ^ c
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
hashCode() you could implement as

return a * b * c;
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
So,

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

Expert Comment

by:derkec
Comment Utility
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
Comment Utility
Definitely true.
(I supposed that as known by the author)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 37

Expert Comment

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

Author Comment

by:krelman
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for accepting.
That keeps us answering your future questions too.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

16 Experts available now in Live!

Get 1:1 Help Now