源码分析(3)-TreeMap(JDK1.8)

一、UML类图

TreeMap类底层数据结构红黑树,键不能为空值可以为空;非线程安全的;可以自定义实现Comparator接口的键比较器。

TreeMap实现NavigableMap接口,NavigableMap接口定义导航方法;同时NavigableMap继承SortedMap接口,SortedMap定义排序方法。

NavigableMap
//返回红黑树小于key的最大键值对 Map.Entry<K,V> lowerEntry(K key); //返回红黑树小于key的最大键 K lowerKey(K key);
//返回红黑树大于key的最小键值对 Map.Entry<K,V> higherEntry(K key); //返回红黑树大于key的最小键 K higherKey(K key);
//返回红黑树小于key的最大键 Map.Entry<K,V> firstEntry(); //返回红黑树小于key的最大键 Map.Entry<K,V> lastEntry();
SortedMap
//红黑树比较器 Comparator<? super K> comparator();
//返回指定键返回的键值对,不包含区间右区间 SortedMap<K,V> subMap(K fromKey, K toKey);
//返回指定键节点所有红黑树头节点键值对 SortedMap<K,V> headMap(K toKey);
//返回指定键节点所有红黑树尾节点键值对,包含key节点 SortedMap<K,V> tailMap(K fromKey);
//返回红黑树第一个节点键 K firstKey();
//返回红黑树最后一个节点键 K lastKey();

 

二、源码分析

 1、成员变量

//比较器
private final Comparator<? super K> comparator;
//根节点
private transient Entry<K,V> root;
//红黑树节点数
private transient int size = 0;
//红黑树结构变化次数
private transient int modCount = 0;

2、插入 

 1 public V put(K key, V value) {
 2         Entry<K,V> t = root;
 3         if (t == null) {//①创建根节点
 4             compare(key, key); // type (and possibly null) check 5 
 6             root = new Entry<>(key, value, null);
 7             size = 1;
 8             modCount++;
 9             return null;
10         }
11         int cmp;
12         Entry<K,V> parent;
13         // split comparator and comparable paths
14         Comparator<? super K> cpr = comparator;//②自定义比较器
15         if (cpr != null) {
16             do {
17                 parent = t;
18                 cmp = cpr.compare(key, t.key);
19                 if (cmp < 0)
20                     t = t.left;
21                 else if (cmp > 0)
22                     t = t.right;
23                 else
24                     return t.setValue(value);
25             } while (t != null);
26         }
27         else {//③默认比较器
28             if (key == null)
29                 throw new NullPointerException();
30             @SuppressWarnings("unchecked")
31                 Comparable<? super K> k = (Comparable<? super K>) key;
32             do {//遍历比较,插入数据
33                 parent = t;
34                 cmp = k.compareTo(t.key);
35                 if (cmp < 0)
36                     t = t.left;
37                 else if (cmp > 0)
38                     t = t.right;
39                 else
40                     return t.setValue(value);
41             } while (t != null);
42         }
43         Entry<K,V> e = new Entry<>(key, value, parent);
44         if (cmp < 0)
45             parent.left = e;
46         else
47             parent.right = e;
48         fixAfterInsertion(e);//④修改红黑树特性
49         size++;
50         modCount++;
51         return null;
52     }

  

3、查找 

 1     public V get(Object key) {
 2         Entry<K,V> p = getEntry(key);//查找键值对节点
 3         return (p==null ? null : p.value);
 4     }
 5 
 6     final Entry<K,V> getEntry(Object key) {
 7         // Offload comparator-based version for sake of performance
 8         if (comparator != null)//自定义比较器
 9             return getEntryUsingComparator(key);
10         if (key == null)
11             throw new NullPointerException();
12         @SuppressWarnings("unchecked")
13         Comparable<? super K> k = (Comparable<? super K>) key;//默认比较器
14         Entry<K,V> p = root;
15         while (p != null) {//遍历查询结果
16             int cmp = k.compareTo(p.key);
17             if (cmp < 0)
18                 p = p.left;
19             else if (cmp > 0)
20                 p = p.right;
21             else
22                 return p;
23         }
24         return null;
25     }

三、红黑树拓展

TODO

posted @ 2020-05-20 15:55  心动如雷  阅读(200)  评论(0)    收藏  举报