TreeMap原理(源码学习)
来源:互联网 发布:磁盘擦除软件 比较 编辑:程序博客网 时间:2024/05/22 13:22
TreeMap是一种实现了有序的Map类型。
TreeMap基于红黑树原理实现。
TreeMap是线程不安全的。
TreeMap实现了 NavigableMap<K,V>接口支持导航方法,TreeMap继承自AbstractMap<K,V>。
put方法原理
1、判断有没有root节点。没有则创建一个root(节点用new Entry<K,V>)表示。modCount++
2、定义一个父节点parent记录节点搜索路径,一个比较记录cmp
3、判断比较器是否存在,不存在则使用默认比较器Comparable对Key进行装饰。key直接调用比较方法。
4、根据比较结果沿左子树和右子树遍历节点,得到与key相等的节点,执行setValue方法。
5、如果一直没有找到节点,因为parent记录了t的搜索路径,所以new Entry<K,V>(key, value, parent),构造新的Entry节点,挂载到parent。
put方法源码
/** * Associates the specified value with the specified key in this map. * If the map previously contained a mapping for the key, the old * value is replaced. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt>. * (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with <tt>key</tt>.) * @throws ClassCastException if the specified key cannot be compared * with the keys currently in the map * @throws NullPointerException if the specified key is null * and this map uses natural ordering, or its comparator * does not permit null keys */ public V put(K key, V value) { Entry<K,V> t = root; if (t == null) { // TBD: // 5045147: (coll) Adding null to an empty TreeSet should // throw NullPointerException // // compare(key, key); // type check root = new Entry<K,V>(key, value, null); size = 1; modCount++; return null; } int cmp; Entry<K,V> parent; // split comparator and comparable paths Comparator<? super K> cpr = comparator; if (cpr != null) { do { parent = t; cmp = cpr.compare(key, t.key); if (cmp < 0) t = t.left; else if (cmp > 0) t = t.right; else return t.setValue(value); } while (t != null); } else { if (key == null) throw new NullPointerException(); Comparable<? super K> k = (Comparable<? super K>) key; do { parent = t; cmp = k.compareTo(t.key); if (cmp < 0) t = t.left; else if (cmp > 0) t = t.right; else return t.setValue(value); } while (t != null); } Entry<K,V> e = new Entry<K,V>(key, value, parent); if (cmp < 0) parent.left = e; else parent.right = e; fixAfterInsertion(e); size++; modCount++; return null; }
0 0
- TreeMap原理(源码学习)
- 深入java集合源码学习系列:TreeMap的实现原理
- 集合源码学习(六):TreeMap
- 集合学习--TreeMap 源码初探
- TreeMap和HashMap源码学习
- TreeMap实现原理及源码分析
- TreeMap实现原理及源码分析
- JDK源码学习(2)-TreeMap源码分析
- JDK源码学习系列10----TreeMap
- JDK源码学习之TreeMap.java分析
- Java源码集合类TreeMap学习2
- java1.8 常用集合源码学习:TreeMap
- treeMap原理
- TreeMap原理
- treemap原理
- TreeMap学习笔记(一)
- TreeMap源码分析(基于JDK1.6)
- Java容器(七):TreeMap源码分析
- js 替换scriptb标签中的src,并且加载执行
- javadoc时候乱码-编码 GBK 的不可映射字符
- 单页面应用
- iis建站
- php导入scv文件
- TreeMap原理(源码学习)
- hdu 1285 确定比赛名次 拓扑排序
- 微机原理==第二章16位和32位微处理器(6)
- Mac OS X快捷键
- Android中jar包常见疑难杂症汇总及解决方案(待完善)
- 查找有环链表的入环结点
- Interruption Source 的意义
- 搭建Redis主从集群搭建、redis-sentinel的配置以及Jedis对Sentinel的使用[ by 嗡汤圆 ]
- js判断是否在微信浏览器中打开