-2

This question already has an answer here:

In an interview it was asked from me to show the implementation of hashcode and equals method in an efficient manner so i have constructed the below pojo but the interviewer said that this is not the correct and the best implementation as it can result in an collision , can you please advise is the below implementation of hashcode is correct or not

public class Emp  {

    String name, job;
    int salary;

    public Emp(String n, String j, int s) {
        this.name = n;
        this.job = j;
        this.salary = s;

    }

    public int hashcode() {
        return name.hashCode() + job.hashCode() + salary;
    }

    public boolean equals(Object o) {

        Emp e1 = (Emp) o;
        return this.name.equals(e1.name) && this.job.equals(e1.job) && this.salary == e1.salary;
    }
    }


2 답변


2

You are using:

  • Two String#hashCode invocations, whose algorithm is debated - see here for an interesting thread
  • You're adding the salary (as int!!) for the final part
  • Finally and most importantly, use a seed: multiply each field by a prime and sum the results

You can look at how your IDE auto-generates the hashcode to get a better idea.

In Eclipse:

  • Right-click on source (or alt-shift-S)
  • Source
  • Generate 'hashCode()' and 'equals()'...


1

Just adding up the hashCode is not a really good idea. There are libraries out there (e.g. Project Lombok) which do this for you. Or you can simply request your IDE to generate one or you. For e.g. Eclipse has an option of generating the hashCode based on the fields in your class.

To extrapolate a bit; let's assume you have the following hashCodes:

name.hashCode() = 200
job.hashCode()  = 400
salary          = 1000000

But you might have another unique employee which ends up having the following hashCodes:

name.hashCode() = 400
job.hashCode()  = 200
salary          = 1000000

As you can see, even though we have two different employees here, we end up with the same hashCode for both. A desirable property of hashCode is to ensure that the entities which you are hashing end up getting distributed as uniformly as possible.

If you look at all the "good" hashCode implementations you will notice multiplication with prime numbers. This ensures that even though the sum of all individual hashCode ends up the same, you still have an overall hashCode which is different. You can try it out with the above example.


  • Remember, this was an interview question, so the interviewer would be looking for a simple solution, directly coded. Not sure use of external library is what was required. - Andrew Fielden
  • @AndrewFielden: I realized that later and have added in few more details. - Sanjay T. Sharma

Linked


Related

Latest