咱们知道,在各种查找方式中,哈希查找的时间复杂度是最小的O(1),由于这种方式并不须要遍历,而是直接计算出数据存储的位置,固然在hashmap中依然继承了这种优势。可是,在hashmap中node的使用也提高了存取速度。
这是源代码中对table数组的定义。node
transient Node<K,V>[] table;
咱们发现,table是一个Node类型的数组,而Node实现了Map的Entry接口,也就是说,它将一条记录和他的关键字绑定在了一块儿,咱们能够经过一个node对象直接获取一对键值对的键和值,而不须要逐个遍历对比来查找他的值数组
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() {… } public final int hashCode() {…} public final V setValue(V newValue) {… } public final boolean equals(Object o) {…} }