HashMap
来源:互联网 发布:手机淘宝老版本官方 编辑:程序博客网 时间:2024/06/05 20:25
HashMap继承自AbstractMap,实现了Map,Cloneable, Serializable接口
//内部以数组的方式存放HashCode值不同的元素,hash值是以key值计算得出transient Node<K,V>[] table;//对于hash值相同的元素,如果相同的个数<8以链表的结构存放,当相同的个数>8时,以红黑树的结构存放。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内部两个重要的方法是putVal、getNode、removeNode
putVal
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //使用默认无参构造方法后,第一次调用put()方法会进行扩容 if ((tab = table) == null || (n = tab.length) == 0) //扩容方法,每次扩容当前长度的两倍 n = (tab = resize()).length; //当前hash值所在的数组位置为空,直接插入到数组中 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); else { //当前数组存在hash值相同的元素,以链表结构插入到对应位置的最后一个元素位置 Node<K,V> e; K k; if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; else if (p instanceof TreeNode) //如果是二叉树,以红黑树进行存储,提高查询速度 e = ((TreeNode<K,V>)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); //链表长度>=7,转为树的结构存储 if (binCount >= TREEIFY_THRESHOLD - 1) treeifyBin(tab, hash); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } if (e != null) { V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) //如果value相同,直接把旧的value覆盖,把旧value返回 e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) //数组容量不够的时候进行扩容 resize(); afterNodeInsertion(evict); return null; }
HashMap不对插入key、value数据进行非空检查,允许有一个key为null的元素存在。
getNode
final Node<K,V> getNode(int hash, Object key) { Node<K,V>[] tab; Node<K,V> first, e; int n; K k; //数组不为空并且数组第一个元素不为空 if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { //是数组上的元素,直接返回 if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k)))) return first; //数组上的元素,有下一个元素存在,即链表 if ((e = first.next) != null) { if (first instanceof TreeNode) //链表以红黑树的结构存储,对红黑树进行查找 return ((TreeNode<K,V>)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); } } //找不到返回null return null; }
removeNode
final Node<K,V> removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable) { Node<K,V>[] tab; Node<K,V> p; int n, index; //对数组元素不为空判断 if ((tab = table) != null && (n = tab.length) > 0 && (p = tab[index = (n - 1) & hash]) != null) { Node<K,V> node = null, e; K k; V v; //要删除的元素在数组中 if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k)))) node = p; //要删除的元素在链表或者红黑树中 else if ((e = p.next) != null) { //要删除的元素在红黑树中 if (p instanceof TreeNode) node = ((TreeNode<K,V>)p).getTreeNode(hash, key); else { //要删除的元素在链表中 do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) { node = e; break; } p = e; } while ((e = e.next) != null); } } if (node != null && (!matchValue || (v = node.value) == value ||(value != null && value.equals(v)))) { //如果在红黑树中,删除红黑树中元素 if (node instanceof TreeNode) ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); else if (node == p) //删除链表中的元素 tab[index] = node.next; else //删除数组中的元素 p.next = node.next; ++modCount; --size; afterNodeRemoval(node); return node; } } 找不到返回null return null; }
clear方法
//clear方法吧数组中的元素置为null,并把size设为0 public void clear() { Node<K,V>[] tab; modCount++; if ((tab = table) != null && size > 0) { size = 0; for (int i = 0; i < tab.length; ++i) tab[i] = null; } }
总结
- HashMap是以(key,value)键值对方式存储数据的,但是其内部以数组、链表、红黑树方式对不同的情况进行存储。
- HashMap根据key获取hashcode,存放到数组中;如果两个元素hashcode值相同,就把值追加到前一个相同hashcode值元素的尾部,以链表形式存储,如果链表过长,则会把链表转为红黑树,提高查询效率。
- HashMap不对插入的数据进行检查,允许出现一个key=null的元素插入,允许value=null;对于插入相同key数据的情况,HashMap会把旧的值覆盖,并返回。
- HashMap不对内部的数据是无序的。
JDK版本1.8.0_92。
阅读全文
0 0
- HashMap
- HashMap
- HashMap
- HashMap
- HashMap
- HashMap
- HashMap
- HashMap
- HashMap
- HashMap
- HashMap
- hashmap
- HashMap
- HashMap
- HashMap
- hashmap
- HashMap
- Hashmap
- RCNN学习笔记(1):Rich feature hierarchies for accurate object detection and semantic segmentation
- TI-CC2640R2-Sensor Controller 开发应用
- Linux for Ubuntu 解决WPS安装缺少libpng12-0的问题
- 搭建FTP服务 搭建Http静态服务器环境
- mysql5.7 启用gtid,导致无法创建和删除表
- HashMap
- CentOS服务器下安装配置SSL
- hibernate_log4j日志记录
- Django学习笔记4-创建模型
- uitableview64像素
- 3.IDA-数据显示窗口(导出窗口、导入窗口、String窗口、...窗口)
- RCNN学习笔记(2):Rich feature hierarchies for accurate object detection and semantic segmentation
- 记录Hibernate的hql操作
- jquery控制文本框<select>大全