HashSet集合中add()的执行过程详解

本文详细剖析了HashSet中add方法的执行流程,包括hash方法的计算规则,add方法如何调用HashMap的put方法,以及在不同情况下table数组的处理,如初始为空时的扩容操作,已存在相同元素时的处理等。
摘要由CSDN通过智能技术生成

一、hash方法

在进行add之前要知道hash方法对各种数据类型的操作规则

public class Test1 {
   

	static final int hash(Object key) {
   
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
	public static void main(String[] args) {
   
		//字符串类型对象
		int hash = hash("tom");
		System.out.println(hash);
		hash = hash(new String("tom"));
		System.out.println(hash);
		//基本类型包装类对象
		hash = hash(10000);
		System.out.println(hash);
		hash = hash(new Integer(10000));
		System.out.println(hash);
		//自定义类型对象
		Student stu = new Student();
		hash = hash(stu);
		System.out.println(hash);
		hash = hash(new Student
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值