简体   繁体   English

如果更改equals方法的返回值,为什么输出会更改?

[英]Why the output changes if we change the return value of equals method?

package com.sample;

import java.util.HashMap;

class Student{
    int id;

    @Override
    public int hashCode() {
        return -1;
    }

    @Override
    public boolean equals(Object obj) {
        return false; // returning false
    }

}

public class MainClass {

    public static void main(String[] args) {
        Student s1=new Student();
        s1.id=123;
        Student s2=new Student();
        s2.id=456;

        HashMap<Student,String> s=new HashMap<Student,String>();


        s.put(s1, "One");
        System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
        s.put(s2, "Two");
        System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());
        s.put(s1, "Three");
        System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());

        System.out.println("after insert");

        System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
        System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());


    }

}



OUTPUT

 < s1 value > One < s1 hashcode > 79430
 < s2 value > Two < s2 hashcode > 84524
 < s1 value > Three < s1 hashcode > 80786814
after insert
 < s1 value > Three < s1 hashcode > 80786814 //printing three for s1
 < s2 value > Two < s2 hashcode > 84524 //printing two for s2

// Now if we change the return type of equals method to true , output changes and both returns three as output. //现在,如果我们将equals方法的返回类型更改为true,则输出会更改,并且都返回三个作为输出。 I am unable to understand why the output changes if we are changing the return type of equals method. 我无法理解如果我们更改equals方法的返回类型,为什么输出会更改。 Please explain with the context of bucket(HashMap) and equals method. 请使用bucket(HashMap)和equals方法的上下文进行说明。

class Student{
    int id;

    @Override
    public int hashCode() {
        return -1;
    }

    @Override
    public boolean equals(Object obj) {
        return true; //returning true
    }

}

public class MainClass {

    public static void main(String[] args) {
        Student s1=new Student();
        s1.id=123;
        Student s2=new Student();
        s2.id=456;

        HashMap<Student,String> s=new HashMap<Student,String>();


        s.put(s1, "One");
        System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
        s.put(s2, "Two");
        System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());
        s.put(s1, "Three");
        System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());

        System.out.println("after insert");

        System.out.println(" < s1 value > "+s.get(s1) + " < s1 hashcode > "+s.get(s1).hashCode());
        System.out.println(" < s2 value > "+s.get(s2) + " < s2 hashcode > "+s.get(s2).hashCode());


    }

}


OUTPUT-

 < s1 value > One < s1 hashcode > 79430
 < s2 value > Two < s2 hashcode > 84524
 < s1 value > Three < s1 hashcode > 80786814
after insert
 < s1 value > Three < s1 hashcode > 80786814 //printing three for s1
 < s2 value > Three < s2 hashcode > 80786814 //printing three for s2

In your first snippet, your equals method always returns false , which means the HashMap considers all Student instances to be unique. 在第一个代码段中, equals方法始终返回false ,这意味着HashMap认为所有Student实例都是唯一的。 Therefore s.get(s1) and s.get(s2) return different values. 因此, s.get(s1)s.get(s2)返回不同的值。

In your second snippet, your equals method always returns true and your hashCode always returns -1, which means the HashMap considers all Student instances to be the same. 在第二个片段中, equals方法始终返回truehashCode始终返回-1,这意味着HashMap将所有Student实例视为相同。 Therefore s.get(s1) and s.get(s2) both return the same value (each call to put overrides the previous value). 因此, s.get(s1)s.get(s2)都返回相同的值(每次调用put覆盖先前的值)。 The value is "Three", since that's the last value you put in the Map (by calling s.put(s1, "Three"); ). 该值为“ Three”,因为这是您在Map中输入的最后一个值(通过调用s.put(s1, "Three"); )。

PS, printing s.get(s1).hashCode() seems pointless, since it's the hashCode of the key ( s1.hashCode() ) that determines the bucket in which the entry will be stored in the HashMap , not the hashCode of the value. PS,打印s.get(s1).hashCode()似乎毫无意义,因为是键( s1.hashCode() )的hashCode决定了将条目存储在HashMap存储桶,而不是该项的hashCode 。值。

BTW, I was initially surprised that your first snippet doesn't return null in all calls to s.get() , since equals always returns false , so the HashMap shouldn't be able to locate a key which is equal to the given key. 顺便说一句,我起初感到惊讶,你的第一个片段没有返回null在所有呼叫s.get()因为equals总是返回false ,所以HashMap不应该是能够找到一个keyequal给定键。 However, checking the source code of HashMap I found that the keys are first compared with == before equals is called, which is why the HashMap could locate your keys. 但是,检查HashMap的源代码后,我发现在调用equals之前先将键与==进行比较,这就是为什么HashMap可以找到您的键的原因。

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

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