一 特性

  • 有序,不允许重复,基于红黑树实现有序性,这里的有序是通过Compare进行比较的有序,并非是添加顺序
  • TreeMap基于红黑树实现,根据键的自然顺序进行排序,或者使用Comparator进行排序。
  • 非同步的,迭代器iterator方法返回的是fail-fastl

    二 属性

    ```java //比较器 private final Comparator<? super K> comparator; //根节点 private transient Entry root; //大小 private transient int size = 0;

//操作次数 private transient int modCount = 0;

  1. <a name="fsaTD"></a>
  2. # 三 重要方法
  3. - fixAfterInsertion
  4. - 红黑树插入之后的旋转过程
  5. <a name="HO442"></a>
  6. # 四 构造方法
  7. ```java
  8. public TreeMap() {
  9. comparator = null;
  10. }
  11. public TreeMap(Comparator<? super K> comparator) {
  12. this.comparator = comparator;
  13. }
  14. //从map初始化
  15. public TreeMap(Map<? extends K, ? extends V> m) {
  16. comparator = null;
  17. putAll(m);
  18. }
  19. public TreeMap(SortedMap<K, ? extends V> m) {
  20. comparator = m.comparator();
  21. try {
  22. buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
  23. } catch (java.io.IOException cannotHappen) {
  24. } catch (ClassNotFoundException cannotHappen) {
  25. }
  26. }

五 添加元素

  1. public V put(K key, V value) {
  2. Entry<K,V> t = root;
  3. //根节点为空的情况初始化
  4. if (t == null) {
  5. compare(key, key); // type (and possibly null) check
  6. root = new Entry<>(key, value, null);
  7. size = 1;
  8. modCount++;
  9. return null;
  10. }
  11. int cmp;
  12. //下边为红黑树的插入
  13. Entry<K,V> parent;
  14. // split comparator and comparable paths
  15. Comparator<? super K> cpr = comparator;
  16. if (cpr != null) {
  17. //正常的二叉树插入流程
  18. do {
  19. parent = t;
  20. cmp = cpr.compare(key, t.key);
  21. if (cmp < 0)
  22. t = t.left;
  23. else if (cmp > 0)
  24. t = t.right;
  25. else
  26. return t.setValue(value);
  27. } while (t != null);
  28. }
  29. else {
  30. if (key == null)
  31. throw new NullPointerException();
  32. @SuppressWarnings("unchecked")
  33. Comparable<? super K> k = (Comparable<? super K>) key;
  34. //无比较器的插入
  35. do {
  36. parent = t;
  37. cmp = k.compareTo(t.key);
  38. if (cmp < 0)
  39. t = t.left;
  40. else if (cmp > 0)
  41. t = t.right;
  42. else
  43. return t.setValue(value);
  44. } while (t != null);
  45. }
  46. Entry<K,V> e = new Entry<>(key, value, parent);
  47. if (cmp < 0)
  48. parent.left = e;
  49. else
  50. parent.right = e;
  51. //这个是红黑树插入之后的旋转流程
  52. fixAfterInsertion(e);
  53. size++;
  54. modCount++;
  55. return null;
  56. }

六 删除元素

  1. public V remove(Object key) {
  2. Entry<K,V> p = getEntry(key);
  3. if (p == null)
  4. return null;
  5. V oldValue = p.value;
  6. deleteEntry(p);
  7. return oldValue;
  8. }
  9. private void deleteEntry(Entry<K,V> p) {
  10. modCount++;
  11. size--;
  12. // If strictly internal, copy successor's element to p and then make p
  13. // point to successor.
  14. if (p.left != null && p.right != null) {
  15. //找到替代的节点
  16. Entry<K,V> s = successor(p);
  17. p.key = s.key;
  18. p.value = s.value;
  19. p = s;
  20. } // p has 2 children
  21. // Start fixup at replacement node, if it exists.
  22. Entry<K,V> replacement = (p.left != null ? p.left : p.right);
  23. if (replacement != null) {
  24. // Link replacement to parent
  25. replacement.parent = p.parent;
  26. if (p.parent == null)
  27. root = replacement;
  28. else if (p == p.parent.left)
  29. p.parent.left = replacement;
  30. else
  31. p.parent.right = replacement;
  32. // Null out links so they are OK to use by fixAfterDeletion.
  33. p.left = p.right = p.parent = null;
  34. // Fix replacement
  35. if (p.color == BLACK)
  36. //修复红黑树
  37. fixAfterDeletion(replacement);
  38. } else if (p.parent == null) { // return if we are the only node.
  39. root = null;
  40. } else { // No children. Use self as phantom replacement and unlink.
  41. if (p.color == BLACK)
  42. fixAfterDeletion(p);
  43. if (p.parent != null) {
  44. if (p == p.parent.left)
  45. p.parent.left = null;
  46. else if (p == p.parent.right)
  47. p.parent.right = null;
  48. p.parent = null;
  49. }
  50. }
  51. }

七 获取元素

  1. final Entry<K,V> getEntry(Object key) {
  2. // Offload comparator-based version for sake of performance
  3. //如果指定了比较器则使用指定比较器进行查找
  4. if (comparator != null)
  5. return getEntryUsingComparator(key);
  6. if (key == null)
  7. throw new NullPointerException();
  8. @SuppressWarnings("unchecked")
  9. Comparable<? super K> k = (Comparable<? super K>) key;
  10. Entry<K,V> p = root;
  11. //遍历红黑树查找 使用comparetor
  12. while (p != null) {
  13. int cmp = k.compareTo(p.key);
  14. if (cmp < 0)
  15. p = p.left;
  16. else if (cmp > 0)
  17. p = p.right;
  18. else
  19. return p;
  20. }
  21. return null;
  22. }