TreeMap的构造函数
 
可以传入 自定义的比较器、Map、SortedMap。
 
put方法:
public V put(K key, V value) {
Entry<K,V> t = root; //得到根节点
if (t == null) { //如果根节点为空
compare(key, key); // type (and possibly null) check
root = new Entry<>(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); //根据比较器 比较当前节点与插入节点的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();
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key; //尝试将key转为Comparable<? super K>类型,就是说 如果没有传入比较器
//,key所在的类需要实现Comparable接口
do {
parent = t;
cmp = k.compareTo(t.key); //尝试用key自己实现的comparteTo方法比较父节点的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<>(key, value, parent); //创建新节点,设置key value 父节点
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e); //插入新节点后 对 红黑树继续宁
size++;
modCount++;
return null;
}
public V put(K key, V value) {
Entry<K,V> t = root; //得到根节点
if (t == null) { //如果根节点为空
compare(key, key); // type (and possibly null) check
root = new Entry<>(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); //根据比较器 比较当前节点与插入节点的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();
@SuppressWarnings("unchecked")
//尝试将key转为Comparable ,如果没有实现此接口,会报错
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);//尝试用key自身的compareTo方法比较
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<>(key, value, parent); //此时,找到插入的位置,创建新的节点,传入参数,以及对父节点的引用
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e); //插入节点后,对红黑树进行相应的变化
size++;
modCount++;
return null;

最新文章

  1. IOS第14天(1,UITabBarController的基本的使用)
  2. ECMAScript 6教程 (三) Class和Module(类和模块)
  3. [Java] 解决spring的xml标签内不能自由增加说明的难题,方便调试、部署时进行批量屏蔽
  4. NBOJv2 Problem 1009 蛤玮的魔法(二分)
  5. fork&amp;exec
  6. 12个你未必知道的CSS小知识
  7. stopWeblogic时提示错误以及无法关闭服务
  8. J2SE知识点摘记(二十五)
  9. UVa 988 - Many Paths, One Destination
  10. iOS 错误 之 Potential leak of an object stored into &#39;cs&#39;
  11. ElasticSearch的安装
  12. user-agent | what is the &quot;user-agent&quot; ?
  13. 03 字符串常用操作方法及For 循环
  14. 【1】C#文件操作之目录操作
  15. performance Counter
  16. Linux服务-ftp
  17. 指定Android Studio编译工程时的源文件编码
  18. 2个 List&lt;T&gt;进行数据合并
  19. 深入理解JVM--JVM垃圾回收机制(转)
  20. Codeforces Round #202 (Div. 2)

热门文章

  1. Go MongoDB官方数据库驱动之增删改查
  2. t100 常用公用變數
  3. gym101480
  4. redis 5.0 CLUSTERDOWN The cluster is down
  5. Redis读写分离(三)
  6. 警告:MySQL-server-5.6.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
  7. eclipse创建springboot项目的三种方法
  8. C# 练习题 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
  9. 使用PATH变量进行Linux权限升级技巧
  10. http请求的几种content-type