13、集合--HashSet相关方法源码解析(等map更新完成之后在进行补充)

相关使用的实例地址:https://www.cnblogs.com/Mrchengs/p/10850333.html

 HashMap的基本解析:https://www.cnblogs.com/Mrchengs/p/10852667.html

1、HashSet set = new HashSet();

    public HashSet() {
        map = new HashMap<>();
    }
    static final long serialVersionUID = -5024744406713321676L;

    private transient HashMap<E,Object> map;

    private static final Object PRESENT = new Object();

此时可以看出HashSet时基于HashMap实现的

HashMap的实现在之后的博文中将进行更新

此时仅需要知道底层的实现时HashMap

首先定义一个长度16的数组

 

2、add(E e):添加方法

    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

 

private static final Object PRESENT = new Object();

HashMap.java

    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;
            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;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            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;
    }

HashMap的基本解析:https://www.cnblogs.com/Mrchengs/p/10852667.html 

 

3、size()方法求长度

    public int size() {
        return map.size();
    }

HashMap.java

  transient int size;
   public int size() {
        return size;
    }

HashMap的基本解析:https://www.cnblogs.com/Mrchengs/p/10852667.html

 

4、contains(Object o)

    public boolean contains(Object o) {
        return map.containsKey(o);
    }

HashMap.java

    public boolean containsKey(Object key) {
        return getNode(hash(key), key) != null;
    }
final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

HashMap的基本解析:https://www.cnblogs.com/Mrchengs/p/10852667.html

 

5、retainAll(Collection c):删除c中没有的元素

AbstractCollection.java

public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        boolean modified = false;
        Iterator<E> it = iterator();
        while (it.hasNext()) {
            if (!c.contains(it.next())) {
                it.remove();
                modified = true;
            }
        }
        return modified;
    }

 

6、remove(Object o):移除指定元素

public boolean remove(Object o) {
        return map.remove(o)==PRESENT;
    }
 private static final Object PRESENT = new Object();

HashMap.java

 public V remove(Object key) {
        Node<K,V> e;
        return (e = removeNode(hash(key), key, null, false, true)) == null ?
            null : e.value;
    }
final Node<K,V> removeNode(int hash, Object key, Object value,
                               boolean matchValue, boolean movable) {
        Node<K,V>[] tab; Node<K,V> p; int n, index;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (p = tab[index = (n - 1) & hash]) != null) {
            Node<K,V> node = null, e; K k; V v;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                node = p;
            else if ((e = p.next) != null) {
                if (p instanceof TreeNode)
                    node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                else {
                    do {
                        if (e.hash == hash &&
                            ((k = e.key) == key ||
                             (key != null && key.equals(k)))) {
                            node = e;
                            break;
                        }
                        p = e;
                    } while ((e = e.next) != null);
                }
            }
            if (node != null && (!matchValue || (v = node.value) == value ||
                                 (value != null && value.equals(v)))) {
                if (node instanceof TreeNode)
                    ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
                else if (node == p)
                    tab[index] = node.next;
                else
                    p.next = node.next;
                ++modCount;
                --size;
                afterNodeRemoval(node);
                return node;
            }
        }
        return null;
    }

HashMap的基本解析:https://www.cnblogs.com/Mrchengs/p/10852667.html

 

7、Clear()方法

    public void clear() {
        map.clear();
    }

HashMap.java

public void clear() {
        Node<K,V>[] tab;
        modCount++;
        if ((tab = table) != null && size > 0) {
            size = 0;
            for (int i = 0; i < tab.length; ++i)
                tab[i] = null;
        }
    }

HashMap的基本解析:https://www.cnblogs.com/Mrchengs/p/10852667.html

posted @ 2019-05-11 22:55  MrChengs  阅读(168)  评论(0编辑  收藏  举报