[英]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.