HashMap源码-Basic hash bin node

Posted fantiantian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HashMap源码-Basic hash bin node相关的知识,希望对你有一定的参考价值。

 /**
     * Basic hash bin node, used for most entries.  (See below for
     * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
     */
    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, 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; }

        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;
        }
    }

 

以上是关于HashMap源码-Basic hash bin node的主要内容,如果未能解决你的问题,请参考以下文章

hashMap 源码解读理解实现原理和hash冲突

JDK源码HashMap源码分析

HashMap源码分析

HashMap重点源码剖析

读源码 HashMap Java8

HashMap源码分析