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 99871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:28:15+00:00 2026-05-11T00:28:15+00:00

When overriding the equals() function of java.lang.Object, the javadocs suggest that, it is generally

  • 0

When overriding the equals() function of java.lang.Object, the javadocs suggest that,

it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

The hashCode() method must return a unique integer for each object (this is easy to do when comparing objects based on memory location, simply return the unique integer address of the object)

How should a hashCode() method be overriden so that it returns a unique integer for each object based only on that object’s properities?

 public class People{    public String name;    public int age;     public int hashCode(){       // How to get a unique integer based on name and age?    } } /*******************************/ public class App{    public static void main( String args[] ){        People mike = new People();        People melissa = new People();        mike.name = 'mike';        mike.age = 23;        melissa.name = 'melissa';        melissa.age = 24;        System.out.println( mike.hasCode() );  // output?        System.out.println( melissa.hashCode(); // output?    } } 
  • 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. 2026-05-11T00:28:16+00:00Added an answer on May 11, 2026 at 12:28 am

    It doesn’t say the hashcode for an object has to be completely unique, only that the hashcode for two equal objects returns the same hashcode. It’s entirely legal to have two non-equal objects return the same hashcode. However, the more unique a hashcode distribution is over a set of objects, the better performance you’ll get out of HashMaps and other operations that use the hashCode.

    IDEs such as IntelliJ Idea have built-in generators for equals and hashCode that generally do a pretty good job at coming up with ‘good enough’ code for most objects (and probably better than some hand-crafted overly-clever hash functions).

    For example, here’s a hashCode function that Idea generates for your People class:

    public int hashCode() {     int result = name != null ? name.hashCode() : 0;     result = 31 * result + age;     return result; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've noticed that the following snippet... @Override public boolean equals(Object otherObject) { ... }
Im overriding an equals() method and I need to know if the object is
I have heard from many places and sources that whenever I override the equals()
I have read that when you override Equals on an class/object you need to
Just overriding Equals in TKey does not help. public override bool Equals(object obj) {
I have always thought that the .equals() method in java should be overridden to
I want to implement an override of equals that compares two Java Queue objects
I want to compare two Java objects without overriding equals method . Since I
I know that when overriding hashcode() and equals() of my persistent entities I should
Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class

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.