[英]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
//现在,如果我们将equals方法的返回类型更改为true,则输出会更改,并且都返回三个作为输出。 我无法理解如果我们更改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
覆盖先前的值)。 该值为“ Three”,因为这是您在Map中输入的最后一个值(通过调用s.put(s1, "Three");
)。
PS,打印s.get(s1).hashCode()
似乎毫无意义,因为是键( s1.hashCode()
)的hashCode
决定了将条目存储在HashMap
存储桶,而不是该项的hashCode
。值。
顺便说一句,我起初感到惊讶,你的第一个片段没有返回null
在所有呼叫s.get()
因为equals
总是返回false
,所以HashMap
不应该是能够找到一个key
是equal
给定键。 但是,检查HashMap
的源代码后,我发现在调用equals
之前先将键与==
进行比较,这就是为什么HashMap
可以找到您的键的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.