简体   繁体   English

java hashcode() 面试题

[英]Interview Question on java hashcode()

I recently attended an interview and was asked the following question.我最近参加了一次面试,被问到以下问题。

There are two objects with same hashcode.有两个具有相同哈希码的对象。 I am inserting these two objects inside a hashmap.我将这两个对象插入到 hashmap 中。

hMap.put(a,a); hMap.put(b,b);

where a.hashCode()==b.hashCode()其中a.hashCode()==b.hashCode()

Now tell me how many objects will be there inside the hashmap?现在告诉我 hashmap 里面会有多少个对象?

I answered there will be only one object,since the hashcodes are equal the two objects will be equal and hashmap will not allow duplicate keys.我回答只有一个 object,因为哈希码相等,所以两个对象将相等,hashmap 不允许重复键。 Please tell me whether my understanding is correct or not?请告诉我我的理解是否正确?

There can be two different elements with the same hashcode.可以有两个不同的元素具有相同的哈希码。 So your answer is incorrect.所以你的答案是不正确的。 The only thing guaranteed is that if two elements have different hashcodes then they are different.唯一可以保证的是,如果两个元素具有不同的哈希码,那么它们就是不同的。 When two elements have the same hashcode then Java uses the equals to further differentation.当两个元素具有相同的哈希码时,Java 使用等号进一步区分。

So the answer is one or two objects.所以答案是一两个对象。

There will either be one or two objects in the hashmap. hashmap 中将有一个或两个对象。

If the two objects are not equal, ie .a.equals(b) , both will be stored.如果两个对象不相等,即.a.equals(b) ,则两者都将被存储。

No, the hashCode is an initial lookup for efficiency, but unless a.equals(b)==true there will be two entries不,hashCode 是效率的初始查找,但除非a.equals(b)==true会有两个条目

There will be two objects in the hashmap, because they are not equals() . hashmap 中会有两个对象,因为它们不是equals()

Here's the proof:这是证明:

public static void main(String[] args) {
    Object a = new Object() {
        public int hashCode() {
            return 1;
        }
    };

    Object b = new Object() {
        public int hashCode() {
            return 1;
        }
    };

    Map<Object, Object> map = new HashMap<Object, Object>();
    map.put(a, a);
    map.put(b, b);
    System.out.println(map.size());
}

Output: Output:

2

If I add an equals() method like this:如果我添加这样的 equals() 方法:

        public boolean equals(Object obj) {
            return obj.hashCode() == hashCode();
        }

Output: Output:

1

According to the javadoc for Object.equals() :根据Object.equals()的javadoc:

Note 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.请注意,每当重写该方法时,通常都需要重写 hashCode 方法,以维护 hashCode 方法的一般约定,即相等的对象必须具有相等的 hash 代码。

This does not however mean that two objects that aren't equals() can't share the same hashcode.然而,这并不意味着不是equals()的两个对象不能共享相同的哈希码。

There are either one or two key objects depending on a.equals(b), and either one or two value objects depending on a==b.取决于 a.equals(b) 有一个或两个键对象,取决于 a==b 有一个或两个值对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM