共同学习Java源代码-数据结构-LinkedHashMap(二)
来源:互联网 发布:阿里云oss是什么 编辑:程序博客网 时间:2024/06/05 16:10
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;
}
这个是在节点被删后的处理方法 在HashMap中属于模板方法
先将参数e强转为LinkedHashMap的Entry类型 然后获取这个节点的before和after节点 固化为b和a
先把before after的引用都清空
判断b如果为空 head节点就设为a 如果b不为空b的after节点为a
判断如果a为空 tail节点设为b 如果a不为空 a的before节点为b
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);
}
}
这个方法是在节点添加后的处理方法 在HashMap中属于模板方法
首先创建Entry类临时变量first
然后判断参数evict是否为true 且 将head赋给first之后 first是否还为空 且调用removeEldestEntry方法返回值是否为true 如果都为true
first的key就赋给一个临时变量key 调用removeNode节点删除key对应的键值对
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;
}
}
这个方法是将节点移动到最后的方法 在HashMap中是模板方法
先创建Entry类临时变量last
判断如果accessOrder为true且将tail赋给last之后last不是参数e
将e强转为Entry对象p 将p的before和after固化为b和a
将p的after清空
判断如果b为空 head赋为a 否则b的after节点赋为a
判断如果a不为空
a的before节点赋为b 否则last节点赋为b
判断如果last为空 head赋为p 否则p的before节点赋为last last的after节点赋为p
tail赋为p
modCount自增
这个方法的意义在于 一旦访问了某个节点 并且accessOrder为true 就将这个节点放在队尾 遍历时最后遍历刚访问的节点
void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
s.writeObject(e.key);
s.writeObject(e.value);
}
}
这个方法是写出键值对各个节点的方法比较简单 不多说了 就是遍历after属性 然后用输出流往外写
- 共同学习Java源代码-数据结构-LinkedHashMap(二)
- 共同学习Java源代码-数据结构-LinkedHashMap(一)
- 共同学习Java源代码-数据结构-LinkedHashMap(三)
- 共同学习Java源代码-数据结构-LinkedHashMap(四)
- 共同学习Java源代码-数据结构-LinkedHashMap(五)
- 共同学习Java源代码-数据结构-TreeSet(二)
- 共同学习Java源代码-数据结构-HashMap(二)
- 共同学习Java源代码--数据结构--AbstractCollection抽象类(二)
- 共同学习Java源代码--数据结构--AbstractList抽象类(二)
- 共同学习Java源代码--数据结构--ArrayList类(二)
- 共同学习Java源代码--数据结构--LinkedList类(二)
- 共同学习Java源代码-数据结构-HashSet(二)
- 共同学习Java源代码-数据结构-HashSet(二)
- 共同学习Java源代码-数据结构-AbstractMap抽象类(二)
- 共同学习Java源代码-数据结构-HashMap(二十)
- 共同学习Java源代码--数据结构--开篇语
- 共同学习Java源代码--数据结构--Collection接口
- 共同学习Java源代码--数据结构--List接口
- JSP总结
- 转载linux相关
- MongoDB and Python.pdf 英文原版 免费下载
- tar -zxvf sdf.tar.gz 失败处理
- Springboot+RabbitMq
- 共同学习Java源代码-数据结构-LinkedHashMap(二)
- IT和一些低级别的编程工作
- 哈夫曼树
- mysql (1)
- java final用法汇总
- secureCRT 8 license
- Android自定义view渐变圆形动画
- 强模拟有毒之蛇形方阵
- OkHttp3Utils