java 학습 노트(5)-HashSet 클래스

4191 단어
1. HashSet 소개
HashSet은 Set 인터페이스의 실현을 바탕으로 내부 저장 데이터는 HashMap을 사용하는데 그 조작은 모두 HashMap을 바탕으로 한다.
2. 몇 가지 중요한 매개 변수
private transient HashMap map;// 
private static final Object PRESENT = new Object();// 
    public HashSet() {
        map = new HashMap<>();// HashMap 16 0.75
    }

    
    public HashSet(int initialCapacity, float loadFactor) {
        map = new HashMap<>(initialCapacity, loadFactor);
    }

    public HashSet(int initialCapacity) {
        map = new HashMap<>(initialCapacity);
    }

    HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
    }


3. 주로 HashSet의dd방법을 본다
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

HashMap의 put 방법을 뚜렷하게 사용하고 HashMap에 위치합니다
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[] tab; Node p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)// key hash table index, p
            tab[i] = newNode(hash, key, value, null);//table , return null
        else {// hash , 
            Node e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))//hash , key , p e, else
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode)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;// PRESENT
                afterNodeAccess(e);
                return oldValue;// PRESENT,HashSet map.put(e, PRESENT)==null false
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;//HashSet 
    }

주의:HashSet에서는 get 방법이 없습니다.HashSet의 요소를 꺼내려면 교체기로 옮겨다닐 수 있습니다
4.contains 방법
    public boolean contains(Object o) {
        return map.containsKey(o);
    }
HashMap.java

    public V get(Object key) {
        Node e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }

    final Node getNode(int hash, Object key) {
        Node[] tab; Node 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;// Object key
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode)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;
    }

좋은 웹페이지 즐겨찾기