If two objects have same hashcode then they may or may not be equal

Hi,

I am reading as below

If two objects have same hashcode then they may or may not be equal


can you please provide simiple example to understand above
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
HashCode in Java is an integer:

public int hashCode()

So a really simple example of how 2 values can have the same hash code but not be equal, is if you hash a Long:

int myHashCode = new Long(25).hashCode() ;

Since there are a *lot* less integers than there are longs:

Integer.MAX_VALUE =  2147483647
Integer.MIN_VALUE = -2147483648

Long.MAX_VALUE =  9223372036854775807
Long.MIN_VALUE = -9223372036854775808

it follows that a lot of the longs must hash to the same hashCode as many other longs.
On average each long value will collide with (generate the same hashcode) as about 2147483647 other longs, even though each long is  a unique value (and so not equal).

Make sense?

Doug
CEHJCommented:
And

System.out.println(new Integer(1).hashCode());
System.out.println(new Long(1).hashCode());

Open in new window


They can't be equal (they different types)
Ravindra76Commented:
To understand it in better terms, assume that

1) You have 10 buckets numbered from 0 to 9.

2) You are using mod of 10  for generating hash

4) Take two number 9 and 19 both will come under same hash : 9

Now hash is same but numbers are not same
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

gudii9Author Commented:

public class Test {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println(new Integer(1).hashCode());
            System.out.println(new Long(1).hashCode());
            //System.out.println((new Integer(1).hashCode()).equals(new Long(1).hashCode()));

      }

}

above gave below output.
1
1


i wonder why they have same hashCode even though different types. Is it is bug in java?
gudii9Author Commented:
2) You are using mod of 10  for generating hash

in real time what mod we use to generate hash?
please advise
CEHJCommented:
i wonder why they have same hashCode even though different types. Is it is bug in java?
No - what makes you think that?

in real time what mod we use to generate hash?

You mean in real life? The best way to find that out is to look at the source code
Ravindra76Commented:
Have a look at : http://stackoverflow.com/questions/113511/best-implementation-for-hashcode-method

Sample code:

public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + (int) (a ^ (a >>> 32));
    result = prime * result + (int) (b ^ (b >>> 32));
    result = prime * result + (int) (c ^ (c >>> 32));
    return result;
}
dpearsonCommented:
i wonder why they have same hashCode even though different types. Is it is bug in java?

Remember the analogy that a hash code is the way you find a book in a library?

Let's say the index in this library is an alphabetic list based on the first letter of the title's name.  So to find a book with a title of "Liar's Poker" you look in the "L" section.

Just like in a real library, you could also store videos in the library and look them up based on the first letter of the title's name, so "Lord of the Rings" would be in the "L" section too.

Two objects - different classes - same hash code.

Make sense?

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.