Effective hashcode function

I have an Employee class with many attributes. One attribute is employeeId , which is of type int .

Can I have a hascode function for Employee as follows?

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

Is it effective?

+4
source share
7 answers

What about:

 return employeeId; 
+17
source

The only requirement for hashCode() is that the return values โ€‹โ€‹are quite unique and that two instances of Employee, equal according to equals() , have the same hash code. Therefore, returning employeeId is the best choice in this case.

To answer the original question, no, it is not very effective. The new construction of the object will give you very little success if you make many calls, although the compiler (and hash implementations) can be smart enough to optimize some of them. Of course, you will only see this if you use it very heavily, which is usually not the case.

+2
source

If employeeId really is of type int , then this should work, and it is significantly 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; those. that two employees (and their corresponding Employee objects) do not have the same employeeId value.

+2
source

I would be a little afraid of the return of raw EmployeeID s. Depending on the company, this can lead to clustering. One obvious picture is that when everything goes well, companies hire a lot of people. Then, when things are not going so well, they drive people away, mostly in the reverse order of seniority. Then, when everything is picked up again, they again hire more people.

This means that you will have relatively tightly filled runs, alternating with relatively sparsely populated runs. Although you are satisfied with the hash requirements and you have created a hash code very quickly, the performance of the hash table of these elements can be adversely affected.

+2
source

No, this is not effective.

A new object will be created when the client calls this hash code ().

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

+1
source

As long as it performs as intended and does not give you any problems, yes.

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

0
source

I would not think so, I know that Java should have the fastest placement of new objects in the west, but this is unnecessary selection. Assuming 2 integer objects with the same value will return the same hash code, I donโ€™t see the reasons why this is not so, but when reading Javadoc, I donโ€™t see the language that would force them to have the same hash code, I believe Findbugs would suggest using

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

since in some cases it is assumed that it will be more effective than the new one. Although I agree that returning an employee identifier would be a better solution. as suggested by Mercy and Stephen S.

0
source

All Articles