HashSet存储实质

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共同指向一个数组对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值