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

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To begin, open a project in Visual Studio and follow… May 11, 2026 at 6:05 pm
  • Editorial Team
    Editorial Team added an answer WCF is a communication stack that allows services to be… May 11, 2026 at 6:05 pm
  • Editorial Team
    Editorial Team added an answer Math.cs and String.cs. I agree it's a bit tricky, though… May 11, 2026 at 6:05 pm

Related Questions

This question is specifically related to overriding the equals() method for objects with a
When running FindBugs on my project, I got a few instances of the error
Background I am using interface-based programming on a current project and have run into
I have this class. public class Foo { public Guid Id { get; set;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.