Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:58:39+00:00 2026-06-09T01:58:39+00:00

Eclipse source menu has a generate hashCode / equals method which generates functions like

  • 0

Eclipse source menu has a “generate hashCode / equals method” which generates functions like the one below.

String name; 
@Override
public int hashCode()
{
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj)
{
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    CompanyRole other = (CompanyRole) obj;
    if (name == null)
    {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

If I select multiple fields when generating hashCode() and equals() Eclipse uses the same pattern shown above.

I am not an expert on hash functions and I would like to know how “good” the generated hash function is? What are situations where it will break down and cause too many collisions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:58:41+00:00Added an answer on June 9, 2026 at 1:58 am

    You can see the implementation of hashCode function in java.util.ArrayList as

    public int hashCode() {
        int hashCode = 1;
        Iterator<E> i = iterator();
        while (i.hasNext()) {
            E obj = i.next();
            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
        }
        return hashCode;
    }
    

    It is one such example and your Eclipse generated code follows a similar way of implementing it. But if you feel that you have to implement your hashCode by your own, there are some good guidelines given by Joshua Bloch in his famous book Effective Java. I will post those important points from Item 9 of that book. Those are,

    1. Store some constant nonzero value, say, 17, in an int variable called result.
    2. For each significant field f in your object (each field taken into account by the equals method, that is), do the following:

      a. Compute an int hash code c for the field:

      i. If the field is a boolean, compute (f ? 1 : 0).

      ii. If the field is a byte, char, short, or int, compute (int) f.

      iii. If the field is a long, compute (int) (f ^ (f >>> 32)).

      iv. If the field is a float, compute Float.floatToIntBits(f).

      v. If the field is a double, compute Double.doubleToLongBits(f), and then hash the resulting long as in step 2.a.iii.

      vi. If the field is an object reference and this class’s equals method compares the field by recursively invoking equals, recursively invoke hashCode on the field. If a more complex comparison is required, compute a “canonical representation” for this field and invoke hashCode on the canonical representation. If the value of the field is null, return 0 (or some other constant, but 0 is traditional)

      vii. If the field is an array, treat it as if each element were a separate field.
      That is, compute a hash code for each significant element by applying
      these rules recursively, and combine these values per step 2.b. If every
      element in an array field is significant, you can use one of the
      Arrays.hashCode methods added in release 1.5.

      b. Combine the hash code c computed in step 2.a into result as follows:

         result = 31 * result + c;
      
    3. Return result.

    4. When you are finished writing the hashCode method, ask yourself whether
      equal instances have equal hash codes. Write unit tests to verify your intuition!
      If equal instances have unequal hash codes, figure out why and fix the problem.

    Java language designers and Eclipse seem to follow similar guidelines I suppose. Happy coding. Cheers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I use the Eclipse source code formatter / beautifier (via the menu item
The next method of java.util.Random , when I view source in Eclipse, is essentially:
Eclipse already has very impressive and useful what I call source code modifiers (please
Noob alert : when I right-click on my eclipse project, the Source menu only
In Eclipse source code, I've found some '$NON-NLS-1$' in comments used like that :
I'd like to create an eclipse plug-in that, when activated (via some extra menu
Currently my eclipse source folder display the entire package path and java files under
I'm using Eclipse/Spring source to edit JSP tags and EL. If I want to
In Spring Source Toolsuite (Eclipse with some Spring tuning) there is nice wizard to
Suppose I have two Java projects in Maven / Eclipse: An open source application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.