HashSet源码学习,基于HashMap实现

HashSet源码学习

一)、Set集合的主要使用类

1). HashSet

  基于对HashMap的封装

2). LinkedHashSet

  基于对LinkedHashSet的封装

3). TreeSet

  基于对TreeSet的封装

注: HashSet、 LinkedHashSet、 TreeSet的实现,都是委托其对应的map对象实现的。

二)、HashSet的特点

1).元素不能重复,元素的输入顺序和输出顺序不同。

2).内部维护一个HashMap对象,所有有关set的实现,都委托HashMap对象完成。

3).有关HashSet的一切性能特性和实现细节与HashMap完全相同。

三)、HashSet的主要属性

//内部对HashMap的封装
private transient HashMap<E,Object> map;
//内部封装的HashMap对应的value都指向Object PRESENT对象
private static final Object PRESENT = new Object();

四)、基于HashMap实现的add(E e)方法

创建一个HashSet对象:

//创建一个HashSet对象 
HashSet<String> set = new HashSet<>();
        System.out.println(set.add("a")); //true
        System.out.println(set.add("a")); //false
        //HashSet的元素不能重复
        System.out.println(set.size()); // 1
        //通过迭代器获取set中的元素
        for(Iterator it = set.iterator() ; it.hasNext() ;  ){
            it.next();
        }



//创建HashMap对象,内部封装了一个HashMap对象
public HashSet() {
        map = new HashMap<>();
    }

添加元素:

set.add("a");

//添加元素
public boolean add(E e) {
        //操作内部封装的HashMap对象,调用put方法
       //key值不重复返回null,重复返回旧的value值
        return map.put(e, PRESENT)==null;
    }
//HashMap的put方法
public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            //判断key值是否重复,若重复e = p ; 不等于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);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    //判断key值是否重复
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            //key值重复的操作
            if (e != null) { // existing mapping for key
                //key值重复,将key对应的value修改为新值
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                //返回旧value
                return oldValue;
            }
        }
        ++modCount;
        //key值不重复,size++, 返回null
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }
金麟岂能忍一世平凡 飞上了青天 天下还依然
原文地址:https://www.cnblogs.com/Auge/p/11662327.html