//Student类
package sun;
public class Student {
private String id;
public Student(String id) {
this.id = id;
}
}
//Test类
package sun;
import java.util.HashSet;
public class Test1 {
public static void main(String[] args) {
HashSet<Student> set = new HashSet<Student>();
set.add(new Student("100"));
set.add(new Student("100"));
}
}
//此时set集合中存入了两个对象
//分析add方法的执行过程
1、HashSet中add方法:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
2、HashMap中put方法:
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//存第一个和第二个值的时候由于hashCode不相同,所以第二次存的对象与第一次存的对象hash(key)结果不一样的
}
//hash方法
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
3、HashMap中putVal方法:
final V putVal(int hash,