java集合之HashSet

特征

HashSet是实现Set,功能上相比ArrayList的特点具有去重的功能。其底层是通过HashMap实现的。正如名字的Hash,HashSet的去重是使用集合元素的hash值来实现的。
在这里插入图片描述
HashMap采用链表+数组形式,每一个元素加入会先通过hash取值,获得数组的位置,再判断该数组的链表位置是否有元素,如果有判断是否相同,相同则跳过,如果不同,将其加入链表末尾

源码分析

下文通过HashSet的add操作,来解读HashSet的功能

//代码debug调试
public class HashSetTest {
    public static void main(String[] args) {
        HashSet hashSet=new HashSet();
        for(int i=0;i<=8;i++){
            hashSet.add(new person("tom",i));
        }
        hashSet.add(new person("tom",1));
        System.out.println(hashSet);

    }
}
class person{
    private String name;
    private int age;

    public person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        person person = (person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

下面按照debug顺序,进行截图和说明
1.进行put操作
判断key,如果返回为null,表示hashset无此元素,可将其加入
在这里插入图片描述
2.调用此方法,将对值进行Hash取值
在这里插入图片描述
3.此方法会首先判断值是否为null,如果是返回0,否则对其hashcode获得的值进行右移操作

在这里插入图片描述
调用重写的hashcode方法,部分
在这里插入图片描述
4 进行去重操作

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        //如果当前hashset为null或长度为0 则进行初始化 
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        //位置进行hash  因为hashset是数组+链表式结构 所以首先通过hash判断数组位置是否为空,为空则将其加入,hash获取数组位置
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            //初始化新节点
            Node<K,V> e; K k;
            //判断是否为null或者内容相同,则直接退出
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            //判断是否为红黑树
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    //遍历链表
                    //如果直到最后的值,都没有找出相同的则直接将值添加到最后
                    if ((e = p.next) == null) {
                        //取出数据 
                        p.next = newNode(hash, key, value, null);
                        //链的长度到8,判断数组长度是否达到64,若否进行扩容,若则进行树化
                        if (binCount >= TREEIFY_THRESHOLD - 1) 
                            treeifyBin(tab, hash);
                        break;
                    }
                    //判断两个的hash值,
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            //如果存在该值,则返回非null
          if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }
    /**
     * Replaces all linked nodes in bin at index for given hash unless
     * table is too small, in which case resizes instead.
     *树化操作
     */
    final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        //判断数组是否为null,判断数组长度是否为64  
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
            resize();
        //key进行tab的hash,判断数组的位置
        else if ((e = tab[index = (n - 1) & hash]) != null) {
            TreeNode<K,V> hd = null, tl = null;
            do {
                TreeNode<K,V> p = replacementTreeNode(e, null);
                if (tl == null)
                    hd = p;
                else {
                    p.prev = tl;
                    tl.next = p;
                }
                tl = p;
            } while ((e = e.next) != null);
            if ((tab[index] = hd) != null)
                hd.treeify(tab);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值