Eclipse can generate Java hashCode and equals methods

A nice feature of Eclipse is that you can easily generate hashCode and equals methods for your Java class using the editor. You'll really appreciate this capability when you need to create these methods whenever you're doing anything related to sorting, comparisons, comparators, etc.

To generate hashCode and equals methods, just have the desired Java class open in the Eclipse editor, then click the Source menu, then choose the "Generate hashCode() and equals()" menu item. Assuming you have class variables available, Eclipse will generate the methods for you. (Of course, like any generated code you'll want to review it, but I've been able to use the generated code in several tests so far.)

Follow up

I've included the source code for a Java class below, so you can see what the class looked like before creating these methods, and then see the hashCode and equals methods Eclipse generated.

 

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Permalink

I try and hashCode method implemantation was:
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
return result;
}

so complex. Why it can't just be like:
public int hashCode() {
return 31 + ( ( id == null ) ? 0 : id.hashCode() );
}

Just wondering.

I think this will make sense in a more-complicated Java class. For instance, I just generated a hash code in Eclipse, starting with this Java class that had a few different fields in it:

public class HashCodeTest
{
  private int i;
  private float f;
  private String s;
  
  public static void main(String[] args)
  {
  }

}

and then when I selected the option to generate a hashCode and equal method, Eclipse generated this hashCode method for me:

@Override
public int hashCode()
{
  final int prime = 31;
  int result = 1;
  result = prime * result + Float.floatToIntBits(f);
  result = prime * result + i;
  result = prime * result + ((s == null) ? 0 : s.hashCode());
  return result;
}

In this case it used my int, float, and String fields to generate the hash code.

For completeness, here's the source code for the equals method that Eclipse generated for this example Java class:

@Override
public boolean equals(Object obj)
{
  if (this == obj)
    return true;
  if (obj == null)
    return false;
  if (getClass() != obj.getClass())
    return false;
  HashCodeTest other = (HashCodeTest) obj;
  if (Float.floatToIntBits(f) != Float.floatToIntBits(other.f))
    return false;
  if (i != other.i)
    return false;
  if (s == null)
  {
    if (other.s != null)
      return false;
  }
  else if (!s.equals(other.s))
    return false;
  return true;
}

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.