public class Test {
static int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Tom");//HashSet存储的数据实质存在了HashMap的key
//1、HashMap类中hash(Object key)分析:
//a、String类型:如果内容相同,则返回值相同
int hash = hash("Tom");
System.out.println(hash);
hash = hash(new String("Tom"));
System.out.println(hash);
//b、基本类型包装类:如果值相同,则返回值相同
hash = hash(10000);
System.out.println(hash);
hash = hash(new Integer(10000));
System.out.println(hash);
//c、自定义类型:如果地址不同则返回结果不同
hash = hash(new Student());
System.out.println(hash);
Student student = new Student();
hash = hash(student);
System.out.println(hash);
hash = hash(student);
System.out.println(hash);
}
}
class Student{
}
//2、HashMap是怎么存的?
//a、当HashMap对象创建后,table全局变量为null
//b、Node数组长度 16 DEFAULT_INITIAL_CAPACITY
//c、tab = resize()//table赋值 table与tab共同指向一个数组对象
HashSet存储实质
最新推荐文章于 2021-03-10 16:09:14 发布