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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:41:40+00:00 2026-06-15T00:41:40+00:00

I know the contract says if two objects are equal then they should return

  • 0

I know the contract says “if two objects are equal then they should return the same hash code”. That is so that those objects can be place in the same hash bucket and to improve hash code related collection functions know. Then again why it says “if two objects have same hash code those should not always be equals”. I mean if it is true in the contract we should say “if two objects are equals they may return same hash code but which is not mandatory”

  • 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-15T00:41:41+00:00Added an answer on June 15, 2026 at 12:41 am

    I mean if it is true in the contract we should say “if two objects are equals they may return same
    hash code but which is not mandatory”

    No, it should not. This is because, whenever an object is searched let’s say in a HashMap or a HashSet, then first it is searched on the basis of hashCode(NOTE : – hashCode() is not used for search in case of ArrayList, or LinkedList. They are not hash based collections), and then if the two objects have same hashcodes, it moves to the equals method to compare the objects themselves.

    Now suppose if the above statement was true, the first test itself would fail for those objects. That is, if two equal objects are allowed to have different hashcode, then while searching for a particular hashCode, it won’t return the correct result, and thus the test will not proceed to equals method, and declare those objects to be unequal even though you expected them to be equal.

    Now lets move to the second statement: –

    if two objects have same hash code those should not always be equals”

    Let’s understand like this: – Since hashCode generated for each object is of type int, so you can generate a maximum 2 ^ 32 unique hashcodes. So, imagine what would happen if you want to store more objects than that. In that case, there has to be a collison for two different objects. So, you would have no other way than to assign same hashCodes to two different objects. And hence the above statement is justified.


    So, two things are quite clear from the above explanation: –

    • Two same objects must have same hashCodes.
    • Two different objects can have same hashCodes.

    More details on this topic in the below link (Nothing can give better explanation than this): –

    • Effective Java – Override hashCode when you override equals
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that C# 4.0 has the Code Contract feature that can be used
I know two approaches to Exception handling, lets have a look at them. Contract
I have a code contract expresses as this - it validates that entity to
I know that there is code like this: http://www.java2s.com/Code/Android/Security/RSAencryptdecryptfunctionRSAECBPKCS1Padding.htm /* Copyright (c) 2010, Sungjin
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
Ok, I know I am missing something here. I have the following operation contract:
The documentation for -hash says it must not change while a mutable object is
I started a little contract job that requires me to use Java. I was
I want to build an iPhone App that should contact a server that is

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.