一步一步解析java集合框架HashMap源码(1)

来源:互联网 发布:上市公司高管薪酬数据 编辑:程序博客网 时间:2024/05/17 21:53

首先看HashMap的底层结构实现:

    static class Node<K,V> implements Map.Entry<K,V> {        final int hash;//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;        }    }
0 0
原创粉丝点击