基于红黑树实现的有序映射
目录
TreeMap继承关系
TreeMap源码解析
TreeMap总结
TreeMap继承了AbstractMap抽象类,拥有map的相关操作方法
TreeMap实现了Serializable接口,支持序列化,可通过序列化传输
TreeMap实现了Cloneable接口,覆盖了clone()方法,能被克隆
TreeMap实现了NavigableMap接口,拥有针对给定搜索目标返回最接近匹配项的导航方法
类Values和EntrySet:
class Values extends AbstractCollection {
/**
* iterator, size, contains, remove, clear, spliterator
*/
}
class EntrySet extends AbstractSet> {
/**
* iterator, size, contains, remove, clear, spliterator
*/
}
静态不可变 类KeySet:
static final class KeySet extends AbstractSet implements NavigableSet {
private final NavigableMap m;
KeySet(NavigableMap map) { m = map; }
public Iterator iterator() {
if (m instanceof TreeMap)
// 返回Treemap类型的键
return ((TreeMap)m).keyIterator();
else
// 返回NavigableSubMap类型的键
return ((TreeMap.NavigableSubMap)m).keyIterator();
}
// 逆序迭代器
public Iterator descendingIterator() {
if (m instanceof TreeMap)
// 返回Treemap类型的逆序迭代器
return ((TreeMap)m).descendingKeyIterator();
else
// 返回NavigableSubMap类型的逆序迭代器
return ((TreeMap.NavigableSubMap)m).descendingKeyIterator();
}
public int size() { return m.size(); }
public boolean isEmpty() { return m.isEmpty(); }
public boolean contains(Object o) { return m.containsKey(o); }
public void clear() { m.clear(); }
public E lower(E e) { return m.lowerKey(e); } // 返回 =key 中的最小键
public E higher(E e) { return m.higherKey(e); } // 返回 >key 中的最小键
public E first() { return m.firstKey(); }
public E last() { return m.lastKey(); }
public Comparator comparator() { return m.comparator(); }
public E pollFirst() {
Map.Entry e = m.pollFirstEntry(); // 抛出第一个条目
return (e == null) ? null : e.getKey(); // 获取其键
}
public E pollLast() {
Map.Entry e = m.pollLastEntry(); // 抛出最后一个条目
return (e == null) ? null : e.getKey(); // 获取其键
}
public boolean remove(Object o) {
int oldSize = size();
m.remove(o);
return size() != oldSize; // 根据KeySet的大小判断有无删除成功
}
/************************* 切割Set得到子集 ************************/
/**
*
* 从 fromElement 到 toElement
* fromInclusive:包不包括 fromElement
* toInclusive:包不包括 toElement
*/
public NavigableSet subSet(E fromElement, boolean fromInclusive,
E toElement, boolean toInclusive) {
return new KeySet<>(m.subMap(fromElement, fromInclusive,
toElement, toInclusive));
}
/**
* 从 首元素 到 toElement
* inclusive:包不包括 toElement
*/
public NavigableSet headSet(E toElement, boolean inclusive) {
return new KeySet<>(m.headMap(toElement, inclusive));
}
/**
* 从 fromElement 到 末尾元素
* inclusive:包不包括 fromElement
*/
public NavigableSet tailSet(E fromElement, boolean inclusive) {
return new KeySet<>(m.tailMap(fromElement, inclusive));
}
/**
* 从 fromElement 到 toElement
* 包括 fromElement, 不包括 toElement
*/
public SortedSet subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
/**
* 不包括toElement
*/
public SortedSet headSet(E toElement) {
return headSet(toElement, false);
}
/**
* 包括fromElement
*/
public SortedSet tailSet(E fromElement) {
return tailSet(fromElement, true);
}
/**
* 返回逆序的Set
*/
public NavigableSet descendingSet() {
return new KeySet<>(m.descendingMap());
}
public Spliterator spliterator() {
return keySpliteratorFor(m);
}
}
抽象 条目迭代器类PrivateEntryIterator:
/**
* Base class for TreeMap Iterators
*/
abstract class PrivateEntryIterator implements Iterator {
Entry next;
Entry lastReturned;
int expectedModCount;
PrivateEntryIterator(Entry first) {
expectedModCount = modCount;
lastReturned = null;
next = first;
}
public final boolean hasNext() {
return next != null;
}
final Entry nextEntry() {
Entry e = next;
if (e == null)
throw new NoSuchElementException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
next = successor(e); // 返回指定条目的继承条目, 若没有继承项, 则返回null
lastReturned = e;
return e;
}
final Entry prevEntry() {
Entry e = next;
if (e == null)
throw new NoSuchElementException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
next = predecessor(e); // 返回指定条目的前任条目, 若没有前任项, 则返回null
lastReturned = e;
return e;
}
public void remove() {
if (lastReturned == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
// deleted entries are replaced by their successors
if (lastReturned.left != null && lastReturned.right != null)
next = lastReturned;
deleteEntry(lastReturned); // 删除最后一条返回的条目
expectedModCount = modCount;
lastReturned = null;
}
}
final class EntryIterator extends PrivateEntryIterator> {
EntryIterator(Entry first) {
super(first);
}
public Map.Entry next() {
return nextEntry();
}
}
final class ValueIterator extends PrivateEntryIterator {
ValueIterator(Entry first) {
super(first);
}
public V next() {
return nextEntry().value;
}
}
final class KeyIterator extends PrivateEntryIterator {
KeyIterator(Entry first) {
super(first);
}
public K next() {
return nextEntry().key;
}
}
final class DescendingKeyIterator extends PrivateEntryIterator {
DescendingKeyIterator(Entry first) {
super(first);
}
public K next() {
return prevEntry().key;
}
public void remove() {
if (lastReturned == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
deleteEntry(lastReturned);
lastReturned = null;
expectedModCount = modCount;
}
}
抽象静态 类NavigableSubMap:
abstract static class NavigableSubMap extends AbstractMap
implements NavigableMap, java.io.Serializable {
private static final long serialVersionUID = -2102997345730753016L;
/**
* The backing map.
*/
final TreeMap m;
/**
* 端点表示为三元组(fromStart, lo, loinclusion)和(toEnd, hi, hiinclusion)
*
* 若fromStart为真, 则最低(绝对)界限是 backing map 的起点, 其他值将被忽略
* 否则若loinclusion为true, 则lo为包容性界, 否则为排他界
*
* 若toEnd为真, 则最高(绝对)界限是 backing map 的终点, 其他值将被忽略
* 否则若hiinclusion为true, 则hi为包容性界, 否则为排他界
*/
final K lo, hi;
final boolean fromStart, toEnd;
final boolean loInclusive, hiInclusive;
NavigableSubMap(TreeMap m,
boolean fromStart, K lo, boolean loInclusive,
boolean toEnd, K hi, boolean hiInclusive) {
if (!fromStart && !toEnd) {
if (m.compare(lo, hi) > 0)
// 起始位置 > 终点位置
throw new IllegalArgumentException("fromKey > toKey");
} else {
// 类型检查
if (!fromStart)
m.compare(lo, lo); // 比较两个键
if (!toEnd)
m.compare(hi, hi); // 比较两个键
}
this.m = m;
this.fromStart = fromStart;
this.lo = lo;
this.loInclusive = loInclusive;
this.toEnd = toEnd;
this.hi = hi;
this.hiInclusive = hiInclusive;
}
// internal utilities
/**
* 是否低于backing map
*/
final boolean tooLow(Object key) {
if (!fromStart) {
// fromStart == false
int c = m.compare(key, lo);
if (c < 0 || (c == 0 && !loInclusive))
// key < lo || (key == lo && loInclusive == false)
// loInclusive == false 则 backing map 不包括lo
return true;
}
return false;
}
/**
* 是否高于backing map
*/
final boolean tooHigh(Object key) {
if (!toEnd) {
// toEnd == false
int c = m.compare(key, hi);
if (c > 0 || (c == 0 && !hiInclusive))
// key > hi || (key == hi && hiInclusive == false)
// hiInclusive == false 则 backing map 不包括hi
return true;
}
return false;
}
/**
* 在 backing map 中(不包括边界)
*/
final boolean inRange(Object key) {
return !tooLow(key) && !tooHigh(key);
}
/**
* 在 backing map 中(包括边界)
*/
final boolean inClosedRange(Object key) {
// fromStart == true || key >= lo
// toEnd == true || key <= hi