LinkedHashMap的底层实现原理_煎丶包的博客-CSDN博客_linkedhashmap底层实现原理
LinkedHashMap的底层实现原理
LinkedHashMap 是 HashMap 的子类,在HashMap存储结构的基础上,使用了一对双向链表来记录添加元素的顺序。LinkedHashMap底层使用的结构与HashMap相同,因为LinkedHashMap继承于HashMap.二者之间的区别就在于LinkedHashMap内部提供了Entry替换HashMap中的Node。
实例化LinkedHashMap时,空参的构造器源码如下
/**
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
* with the default initial capacity (16) and load factor (0.75).
*/
public LinkedHashMap() {
super();
accessOrder = false;
}
调用put()方法存放元素时,实际上是调用的父类HashMap的put方法,在LinkedHashMap中重写了newNode()方法,同时LinkedHashMap的Entry继承了HashMap的Node。
Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
LinkedHashMap.Entry<K,V> p =
new LinkedHashMap.Entry<K,V>(hash, key, value, e);
linkNodeLast(p);
return p;
}
/**
* HashMap.Node subclass for normal LinkedHashMap entries.
*/
static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after;
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}
LinkedHashMap的底层实现原理_煎丶包的博客-CSDN博客_linkedhashmap底层实现原理
1.概述
在使用HashMap的时候,可能会遇到需要按照当时put的顺序来进行哈希表的遍历。通过上篇对HashMap的了解,我们知道HashMap中不存在保存顺序的机制。本篇文章要介绍的LinkedHashMap专为此特性而生。在LinkedHashMap中可以保持两种顺序,分别是插入顺序和访问顺序,这个是可以在LinkedHashMap的初始化方法中进行指定的。相对于访问顺序,按照插入顺序进行编排被使用到的场景更多一些,所以默认是按照插入顺序进行编排。
看一下实际的运行效果,测试代码如下:
public class LinkHashMapTest {
@Test
public void mapTest() {
Map<String, String> test = new LinkedHashMap<String, String>(16);
test.put("化学", "93");
test.put("数学", "98");
test.put("生物", "92");
test.put("英语", "97");
test.put("物理", "94");
test.put("历史", "96");
test.put("语文", "99");
test.put("地理", "95");
for (Map.Entry entry : test.entrySet()) {
System.out.println(entry.getKey().toString() + ":" + entry.getValue().toString());
}
}
}
运行结果如下图所示,可以看到,输出的顺序与插入的顺序是一致的。
访问顺序
public class LinkHashMapTest {
@Test
public void mapTest() {
//默认是插入顺序,改为访问顺序,按照访问顺序升序排列,访问顺序相同的,按照插入顺序排列
Map<String, String> test = new LinkedHashMap<String, String>(16, 0.75f,true);
test.put("化学", "93");
test.put("数学", "98");
test.put("生物", "92");
test.put("英语", "97");
test.put("物理", "94");
test.put("历史", "96");
test.put("语文", "99");
test.put("地理", "95");
int i = 0;
while (i++ < 100){
test.get("化学");
test.get("物理");
}
for (Map.Entry entry : test.entrySet()) {
System.out.println(entry.getKey().toString() + ":" + entry.getValue().toString());
}
}
}
打印结果
2.原理
- 在LinkedHashMap中,是通过双联表的结构来维护节点的顺序的。上文中的程序,实际上在内存中的情况如下图所示,每个节点都进行了双向的连接,维持插入的顺序(默认)。
head
指向第一个插入的节点,tail
指向最后一个节点。 - LinkedHashMap是HashMap的亲儿子,直接继承HashMap类。LinkedHashMap中的节点元素为
Entry<K,V>
,直接继承HashMap.Node<K,V>
。UML类图关系如下:
3.源码分析
3.1 节点构造方法
刚刚看LinkedHashMap的实现的时候有个疑问。LinkedHashMap继承HashMap,HashMap中的数组是Node<K,V>[]
类型的,在LinkedHashMap中定义了Entry<K,V>
继承Node<K,V>[]
,但是在LinkedHashMap中并没有找到新建节点的方法。仔细研究之后发现,在HashMap类的put
方法中,新建节点是使用的newNode
方法。而在LinkedHashMap没有重写父类的put
方法,而是重写了newNode
方法来构建自己的节点对象。
HashMap中的newNode
方法:
Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
return new Node<>(hash, key, value, next);
}
LinkedHashMap中的newNode
方法:
Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
LinkedHashMap.Entry<K,V> p =
new LinkedHashMap.Entry<K,V>(hash, key, value, e);
linkNodeLast(p);
return p;
}
3.2 put方法
在LinkedHashMap类使用的仍然是父类HashMap的put方法,所以插入节点对象的流程基本一致。不同的是,LinkedHashMap重写了afterNodeInsertion
和afterNodeAccess
方法。
afterNodeInsertion
方法用于移除链表中的最旧的节点对象,也就是链表头部的对象。但是在JDK1.8版本中,可以看到removeEldestEntry
一直返回false
,所以该方法并不生效。如果存在特定的需求,比如链表中长度固定,并保持最新的N的节点数据,可以通过重写该方法来进行实现。
void afterNodeInsertion(boolean evict) { // possibly remove eldest
LinkedHashMap.Entry<K,V> first;
if (evict && (first = head) != null && removeEldestEntry(first)) {
K key = first.key;
removeNode(hash(key), key, null, false, true);
}
}
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
return false;
}
afterNodeAccess
方法实现的逻辑,是把入参的节点放置在链表的尾部。
void afterNodeAccess(Node<K,V> e) { // move node to last
LinkedHashMap.Entry<K,V> last;
if (accessOrder && (last = tail) != e) {
LinkedHashMap.Entry<K,V> p =
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.after = null;
if (b == null)
head = a;
else
b.after = a;
if (a != null)
a.before = b;
else
last = b;
if (last == null)
head = p;
else {
p.before = last;
last.after = p;
}
tail = p;
++modCount;
}
}
3.3 get方法
LinkedHashMap中的get方法与父类HashMap处理逻辑相似,不同之处在于增加了一处链表更新的逻辑。如果LinkedHashMap中存在要寻找的节点,那么判断如果设置了accessOrder
,则在返回值之前,将该节点移动到对应桶中链表的尾部。
public V get(Object key) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) == null)
return null;
if (accessOrder)
afterNodeAccess(e);
return e.value;
}
3.4 remove方法
LinkedHashMap重写了afterNodeRemoval
方法,用于在删除节点的时候,调整双链表的结构。
void afterNodeRemoval(Node<K,V> e) { // unlink
LinkedHashMap.Entry<K,V> p =
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.before = p.after = null;
if (b == null)
head = a;
else
b.after = a;
if (a == null)
tail = b;
else
a.before = b;
}
4.小结
LinkedHashMap相对于HashMap,增加了双链表的结果(即节点中增加了前before、后after指针,LinkedHashMap中增加了head、tail指正),其他处理逻辑与HashMap一致,同样也没有锁保护,多线程使用存在风险。
作者:道可
链接:https://www.imooc.com/article/23169
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作