??????.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;
}