MapClassDiagram
SortedMap
SortedMap提供了对key的排序,key必须是可比较的。两种实现:1、key 实现comparable接口。2、提供comparator.
(1)由于key的有序性,接口定义了一些获取子视图的方法:
subMap,headMap,tailMap,firstKey,lastKey.(都是左开右闭区间)
NavigableMap
(1)继承SortedMap,同样利用key的有序性,提供特定查询。比如:
lowerEntry, floorEntry, ceilingEntry, higherEntry等。
(2)提供了descendingMap。
TreeMap
TreeMap是基于红黑树的实现。在log(n)时间内完成:containsKey,get,put,remove等操作。
(1)B树系列参考
http://www.cnblogs.com/richard-zhouxu/p/B_Tree.html
(2)红黑树介绍参考:
http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf
(3)如果没有指定comparator, key值不允许为空。(此时使用natural-ording, key implements comparable).
(4)TreeMap.Entry如下:
static final class Entry<K,V> implements Map.Entry<K,V> {
K key;
V value;
Entry<K,V> left = null;
Entry<K,V> right = null;
Entry<K,V> parent;
boolean color = BLACK;
/**
* Make a new cell with given key, value, and parent, and with
* <tt>null</tt> child links, and BLACK color.
*/
Entry(K key, V value, Entry<K,V> parent) {
this.key = key;
this.value = value;
this.parent = parent;
}
(5)private transient KeySet<K> navigableKeySet = null;
可以有序迭代key, 并通过key来获取相应的value。
(6)descendingMap,descendingKeySet,descendingKeyIterator等方式进行反向访问。
DescendingSubMap内部类中使用Collections.reverseOrder(m.comparator);来反转比较器。
(7)其它具体的操作参见Red-BlackTree
浙公网安备 33010602011771号