two unequal objects with same hashcode

2020-01-30 07:00发布

Hashcode() and equals() concept is

1) If two Objects are equal according to equal(), then calling the hashcode method on each of those two objects should produce same hashcode.

and other one is

2) It is not required that if two objects are unequal according to the equal(), then calling the hashcode method on each of the two objects must produce distinct values.

I tried and understood first one and this is the code for first point.

public class Test {
    public static void main(String[] args) {

        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(1, 11);
        map.put(4, 11);
        System.out.println(map.hashCode());
        Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
        map1.put(1, 11);
        map1.put(4, 11);
        System.out.println(map1.hashCode());
        if (map.equals(map1)) {
            System.out.println("equal ");
        }
    }
}

the above program gives same hashcode for two different objects.

Can someone explain me with an example,how can two different objects which are unequal according to the equals() have same hashcode.

9条回答
Ridiculous、
2楼-- · 2020-01-30 07:36

2) It is not required that if two objects are unequal according to the equal(), then calling the hashcode method on each of the two objects must produce distinct values.

Depending on the hashing function, 2 different objects can have the same hash code. However, 2 objects which are the same must produce the same result when hashed (unless someone implemented a hashing function with random numbers in which case it's useless)

For example, if I am hashing integers and my hashing function is simply (n % 10) then the number 17 and the number 27 will produce the same result. This does not mean that those numbers are the same.

查看更多
等我变得足够好
3楼-- · 2020-01-30 07:36

Actullay, this link explain what happens if hashcode equals more clearly.

http://www.javamadesoeasy.com/2015/02/hashmap-custom-implementation.html

查看更多
乱世女痞
4楼-- · 2020-01-30 07:37

My understanding is that hashCode is a numeric representation of the memory address, but is not the actual address. It can be changed, without affecting the actual address. Thus, it should be possible to set all objects to the same hashCode, even if they are all entirely different things. Think of everybody on one block all suddenly having the same street address. They are truly different people, but now all share the same street address. Their house didn't move, a mischevious teen just labeled everybody as "100 N. Main".

I am pretty new to Java, so take my reply with a bit of caution.

查看更多
相关推荐>>
5楼-- · 2020-01-30 07:39

I believe it will help you understand...

The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure. We know that hash code is an unique id number allocated to an object by JVM. But actually speaking, Hash code is not an unique number for an object. If two objects are equals then these two objects should return same hash code. So we have to implement hashcode() method of a class in such way that if two objects are equals, ie compared by equal() method of that class, then those two objects must return same hash code. If you are overriding hashCode you need to override equals method also.

ref: https://www.java2novice.com/java_interview_questions/hashcode/

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-01-30 07:44

I't pretty simple to understand if you know how a HashMap is implemented and it's purpose. A Hashmap takes a large set of values, and splits them into much smaller sets(buckets) for much faster retrieval of elements. Basically you only need to search the one bucket instead of the full list for your element. The buckets are in an array where the index is the hash code. Each bucket contains a linked list of elements with the same hashcode, but are not equal(). I think in Java 8 they switched to using a treemap when the bucket sizes becomes large.

查看更多
欢心
7楼-- · 2020-01-30 07:46

It's pretty simple actually,

First we have to know what a hash code is.

In java, a hash code is simple a 32 bit signed integer that is somehow derived from the data in question. The integer types are usually just (Int Data) Mod (some reasonable large prime number).

Let's do a simple hash on integers.
Define:

public int hash(int num){ return num % 19 ; } 

In this case, both 19 and 38 will return the hash value of 0.

For string types, the hash is derived from the individual characters and each ones position in the string, divided by a reasonably large number. (Or, in the case of Java, ignoring overflow in a 32 bit sum).

Given that there are arbitrarily many strings possible, and there is a limited number of hashcodes (2^32) for a string, the pigeon-hole principle states that there are at least two different strings that result in the same hashcode.

查看更多
登录 后发表回答