JDK7 HashMap(一)

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

 哈希类集合三个基本存储概念

  • table,存储所有节点数据的数组
  • slot 哈希槽,即 table[i] 的位置
  • bucket 哈希桶,即 table[i] 上所有元素形成的或者集合

  存储所有节点的table数组
       transient Node<K,V>[] table;

  描述一个hash节点信息:

      static class Node<K,V> implements Map.Entry<K,V> {
        // hash值
        final int hash;
        // 键
        final K key;
        // 值
        V value;
        // 下一个节点
        HashMap.Node<K,V> next;

        Node(int hash, K key, V value, HashMap.Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public final K getKey()        { return key; }
        public final V getValue()      { return value; }
        public final String toString() { return key + "=" + value; }

        // 计算hashcode
        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        // 相等性判断
        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                        Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }

 

猜你喜欢

转载自blog.csdn.net/qq_34579060/article/details/94010487