??Java?е?hashcode????
???????????? ???????[ 2014/5/28 11:34:20 ] ????????Java hashcode
??????.equals??????hashCode????
????????Щ????£??????????????????????????????дequals??????????String?????????????????дequals????????????????дhashCode????????????????
???????濴????????
package com.cxh.test1;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
class People{
private String name;
private int age;
public People(String name??int age) {
this.name = name;
this.age = age;
}
public void setAge(int age){
this.age = age;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return this.name.equals(((People)obj).name) && this.age== ((People)obj).age;
}
}
public class Main {
public static void main(String[] args) {
People p1 = new People("Jack"?? 12);
System.out.println(p1.hashCode());
HashMap<People?? Integer> hashMap = new HashMap<People?? Integer>();
hashMap.put(p1?? 1);
System.out.println(hashMap.get(new People("Jack"?? 12)));
}
}
|
???????????????д??equals???????????????People???????????????????????????????????????
??????δ????????????????δ??????????“1”????????????????????“null”?????????????????дequals??????????????дhashCode??????
????????????дequals????????????????????????????????????ж???????????String????????????????????????£?hashCode????????????洢?????????????????????????????“null”?????????????????p1????????
????System.out.println(hashMap.get(new People("Jack"?? 12)));????е?new People("Jack"?? 12)????????????????????洢?????????????????HashMap??get?????????????
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K??V> e = table[indexFor(hash?? table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
|
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11