tags:

views:

156

answers:

7

I have a Employee class with many attributes. One of the attributes is employeeId which is of type int.

Can I have hascode function for Employee as follows?

int hashCode(){
    return new Integer(employeeId).hashCode();
}

Is it efficient?

+17  A: 

How about:

return employeeId;
Mark Byers
is it efficient?
ako
Yes, returning a single integer is efficient.
Ben S
I don't think you can get much more efficient than that. :) Assuming your employeeIds are all unique, this is a good approach.
Jeff
@ako: Not only is it efficient, it's the exact same value as `Integer.hashCode()` would give you anyway.
Mark Peters
@ako: please use a prime number while calculating hashcode. also do read the effective java by joshua bloch on hashcode. basically to construct a hashcode for an employee , you have to consider all the attributes of the employee such as his age,name,address.
Suresh S
@Suresh S: Actually, no that is dangerous. You should only use immutable attributes when considering a hash code otherwise mutating an attribute of an employee while it is a key in a hash map or it is a member of a hash set will cause it to be in the wrong bucket.
JeremyP
Also, since, the employee id almost certainly uniquely identifies an employee, it would seem to me to be an ideal hash code, since you are guaranteed no collisions.
JeremyP
A: 

As long as it's performing as intended, and not giving you any issues, then yes.

EDIT: Look at http://mindprod.com/jgloss/hashcode.html and http://www.javamex.com/tutorials/collections/hash_function_guidelines.shtml

Parker
@ako please consider what @Parker says! this will get you on the right path!
thelost
@thelost and @ parker: can u giv me some efficent hascode functions?
ako
@ako, sure thing, I updated my answer for you
Parker
+2  A: 

The only requirement for hashCode() is that the values it returns are sufficiently unique and that two instances of Employee who are equal according to equals() have the same hash code. So returning employeeId is the better choice in this case.

To answer the original question, no it is not very efficient. The new object construction will give you a very small performance hit if you're doing a lot of calls, though the compiler (and implementations that use the hash code) might be smart enough to optimize some of that away. Of course you will only see this if you make very heavy use of it, which you typically do not.

wds
+2  A: 

If employeeId is really of type int, then this should work and it is considerably more efficient than your version ... which creates an Integer object for no good reason:

public int hashCode() {
    return employeeId;
}

If employeeId is an integer represented as a String, then the following might be better than your version.

public int hashCode() {
    return Integer.parseInt(employeeId);
}

All of these solutions (including yours) assume that employeeId is a unique key; i.e. that no two employees (and their corresponding Employee objects) have the same employeeId value.

Stephen C
+2  A: 

No,it's not efficient.

An new object will be create when a client invoke this hashCode().

Your employeeId is a int type , it just returns it,does not need to new Integer Object.

Mercy
A: 

I wouldn't think so, I know Java is supposed to have the fastest new object allocation in the west, but its an unnecessary allocation. Assuming that 2 integer objects with the same value would return the same hashcode, I don't see a reason it wouldn't, but reading the Javadoc, I don't see any language that would force them to have the same hashcode, I believe Findbugs would suggest using

return Integer.valueOf(employeeId).hashCode();

as that is supposed to be more efficient than new in some instances. Though I agree that returning the employee id would be the best solution. as Mercy and Steven C have suggested.

mezmo
+2  A: 

I would be a little wary of returning raw EmployeeIDs. Depending on the company, doing so could easily lead to clustering. One obvious pattern is that when things are going well, companies hire a lot of people. Then, when things aren't going so well, they lay people off, largely in reverse order of seniority. Then, when things pick back up again, they hire more people again.

This means you'll tend to have relatively densely populate runs alternating with relatively sparsely populated runs. Even though you've met the requirements for hashing to work and you've generated a hash code very quickly, performance of a hash table of these items may be adversely affected.

Jerry Coffin
To amplify that point: someone might see that the hashcodes look like EmployeeIDs and assume they'll always be the same; someone else might not realize someone else depends upon such behavior and change the meaning of hashcode. It may be good to xor the employeeId with some arbitrary constant when generating the hashcode. One might even change the xor value each system startup (since HashCodes shouldn't be persisted longer than that).
supercat
One would hope that the underlying algorithm used to map a hashcode onto a bucket would take into account this possibility. I think it's a bad idea to try to second guess the Java library.
JeremyP