gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:57:16 26 4
gpt4 key购买 nike

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

//现在,如果我们将 equals 方法的返回类型更改为 true ,输出就会发生变化,并且都返回 3 作为输出。我无法理解如果我们更改 equals 方法的返回类型,为什么输出会发生变化。请结合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

最佳答案

在你的第一个片段中,你的 equals方法始终返回 false ,这意味着HashMap考虑所有Student实例是唯一的。因此s.get(s1)s.get(s2)返回不同的值。

在你的第二个片段中,你的 equals方法始终返回 true和你的hashCode始终返回 -1,这意味着 HashMap考虑所有Student实例必须相同。因此s.get(s1)s.get(s2)两者都返回相同的值(每次调用 put 都会覆盖以前的值)。该值为“三”,因为这是您在 map 中输入的最后一个值(通过调用 s.put(s1, "Three"); )。

附注,打印s.get(s1).hashCode()似乎毫无意义,因为它是 hashCode确定条目将存储在 s1.hashCode() 中的存储桶的键 ( HashMap ) ,而不是 hashCode的值。

顺便说一句,我最初很惊讶你的第一个片段没有返回 null在所有调用s.get()的电话中,自 equals总是返回false ,所以HashMap应该无法找到 key这是 equal到给定的键。然而,查看HashMap的源代码我发现 key 首先与 == 进行比较之前equals被调用,这就是 HashMap 的原因可以找到你的 key 。

关于java - 为什么如果改变 equals 方法的返回值,输出会改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com