最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java基礎之TreeMap實現(xiàn)類全面詳解

 更新時間:2023年12月10日 15:24:24   作者:bug生產者  
這篇文章主要為大家介紹了java基礎之TreeMap實現(xiàn)類全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

TreeMap詳解

TreeMap是Map接口的一個實現(xiàn)類,底層基于紅黑樹的實現(xiàn),按照key的順序存儲

從繼承結構可以看到TreeMap除了繼承了AbstractMap類,還實現(xiàn)了NavigableMap接口,而NavigableMap接口是繼承自SortedMap接口的,所以TreeMap是可以進行排序的

關鍵變量

// 比較器,根據(jù)比較器來決定TreeMap的排序,如果為空,按照key做自然排序(最小的在根節(jié)點)
private final Comparator<? super K> comparator;
// 根節(jié)點
private transient Entry<K,V> root;
/**
 * The number of entries in the tree
 * 樹的大小
 */
private transient int size = 0;
/**
 * The number of structural modifications to the tree.
 * 修改次數(shù)
 */
private transient int modCount = 0;
// Entry為TreeMap的內部類
static final class Entry<K,V> implements Map.Entry<K,V> {
        K key;
        V value;
        Entry<K,V> left;
        Entry<K,V> right;
        Entry<K,V> parent;
        boolean color = BLACK;
}

構造函數(shù)

// 默認空參構造器,比較器設置為空
public TreeMap() {
    comparator = null;
}
// 提供比較器
public TreeMap(Comparator<? super K> comparator) {
  this.comparator = comparator;
}
public TreeMap(Map<? extends K, ? extends V> m) {
  comparator = null;
  putAll(m);
}
public TreeMap(SortedMap<K, ? extends V> m) {
  comparator = m.comparator();
  try {
    buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
  } catch (java.io.IOException cannotHappen) {
  } catch (ClassNotFoundException cannotHappen) {
  }
}

get方法

public V get(Object key) {
    Entry<K,V> p = getEntry(key);
    return (p==null ? null : p.value);
}
final Entry<K,V> getEntry(Object key) {
  // Offload comparator-based version for sake of performance
  if (comparator != null)
    return getEntryUsingComparator(key);
  // 從這里可以看出TreeMap的key不可以為null
  if (key == null)
    throw new NullPointerException();
  @SuppressWarnings("unchecked")
  Comparable<? super K> k = (Comparable<? super K>) key;
  // 獲取根節(jié)點
  Entry<K,V> p = root;
  while (p != null) {
    // 判斷是根節(jié)點的左子樹還是右子樹
    int cmp = k.compareTo(p.key);
    if (cmp < 0)
      p = p.left;
    else if (cmp > 0)
      p = p.right;
    else
      return p;
  }
  return null;
}

put方法

public V put(K key, V value) {
    Entry<K,V> t = root;
      // 根節(jié)點為null,表示這是第一個元素
    if (t == null) {
          // 主要是為了確保key是可排序的類,以及key不能為null
        compare(key, key); // type (and possibly null) check
                // 第三個參數(shù)為父節(jié)點的entry,根節(jié)點沒有父節(jié)點,所以為null
        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);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
      // 不存在比較器,進行自然排序
    else {
          // key不能為null
        if (key == null)
            throw new NullPointerException();
        @SuppressWarnings("unchecked")
            Comparable<? super K> k = (Comparable<? super K>) key;
      // do...while是為了找到該key所要存放的位置(找到父節(jié)點)
        do {
            parent = t;
            cmp = k.compareTo(t.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);
      // 比父節(jié)點小,是左子樹
    if (cmp < 0)
        parent.left = e;
    else
        parent.right = e;
      // 插入之后還要進行平衡操作
    fixAfterInsertion(e);
    size++;
    modCount++;
    return null;
}
private void fixAfterInsertion(Entry<K,V> x) {
  x.color = RED;
  while (x != null && x != root && x.parent.color == RED) {
    if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
      Entry<K,V> y = rightOf(parentOf(parentOf(x)));
      if (colorOf(y) == RED) {
        setColor(parentOf(x), BLACK);
        setColor(y, BLACK);
        setColor(parentOf(parentOf(x)), RED);
        x = parentOf(parentOf(x));
      } else {
        if (x == rightOf(parentOf(x))) {
          x = parentOf(x);
          rotateLeft(x);
        }
        setColor(parentOf(x), BLACK);
        setColor(parentOf(parentOf(x)), RED);
        rotateRight(parentOf(parentOf(x)));
      }
    } else {
      Entry<K,V> y = leftOf(parentOf(parentOf(x)));
      if (colorOf(y) == RED) {
        setColor(parentOf(x), BLACK);
        setColor(y, BLACK);
        setColor(parentOf(parentOf(x)), RED);
        x = parentOf(parentOf(x));
      } else {
        if (x == leftOf(parentOf(x))) {
          x = parentOf(x);
          rotateRight(x);
        }
        setColor(parentOf(x), BLACK);
        setColor(parentOf(parentOf(x)), RED);
        rotateLeft(parentOf(parentOf(x)));
      }
    }
  }
  root.color = BLACK;
}

remove方法

public V remove(Object key) {
      // 獲取到該key對應的節(jié)點 和get相同
    Entry<K,V> p = getEntry(key);
    if (p == null)
        return null;
    V oldValue = p.value;
    deleteEntry(p);
    return oldValue;
}
private void deleteEntry(Entry<K,V> p) {
  modCount++;
  size--;
  // If strictly internal, copy successor's element to p and then make p
  // point to successor.
  // 存在兩個子樹(左子樹和右子樹)
  if (p.left != null && p.right != null) {
    // 找到與p數(shù)值最接近的節(jié)點(即右子樹的最左葉子節(jié)點)
    Entry<K,V> s = successor(p);
    p.key = s.key;
    p.value = s.value;
    p = s;
  } // p has 2 children
  // Start fixup at replacement node, if it exists.
  // 找到所要替代的節(jié)點
  Entry<K,V> replacement = (p.left != null ? p.left : p.right);
  if (replacement != null) {
    // Link replacement to parent
    // 替換節(jié)點
    replacement.parent = p.parent;
    if (p.parent == null)
      root = replacement;
    else if (p == p.parent.left)
      p.parent.left  = replacement;
    else
      p.parent.right = replacement;
    // Null out links so they are OK to use by fixAfterDeletion.
    p.left = p.right = p.parent = null;
    // Fix replacement
    // 刪除的節(jié)點為黑色節(jié)點,需要進行平衡
    if (p.color == BLACK)
      fixAfterDeletion(replacement);
  } 
  // 此時replacement為null(表明 p沒有左子樹也沒有右子樹),如果p沒有父節(jié)點,表明該樹只有一個根節(jié)點
  else if (p.parent == null) { // return if we are the only node.
    root = null;
  } 
  // 此時replacement為null(表明 p沒有左子樹也沒有右子樹),表明該節(jié)點為葉子節(jié)點
  else { //  No children. Use self as phantom replacement and unlink.
    // 刪除的節(jié)點為黑色節(jié)點,需要進行平衡
    if (p.color == BLACK)
      fixAfterDeletion(p);
        // 將p從樹中移除
    if (p.parent != null) {
      if (p == p.parent.left)
        p.parent.left = null;
      else if (p == p.parent.right)
        p.parent.right = null;
      p.parent = null;
    }
  }
}
static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {
  if (t == null)
    return null;
  else if (t.right != null) {
    // 右節(jié)點不為null,找到后繼節(jié)點(即右子樹的左葉子節(jié)點)
    Entry<K,V> p = t.right;
    while (p.left != null)
      p = p.left;
    return p;
  } else {
    Entry<K,V> p = t.parent;
    Entry<K,V> ch = t;
    while (p != null && ch == p.right) {
      ch = p;
      p = p.parent;
    }
    return p;
  }
}
private void fixAfterDeletion(Entry<K,V> x) {
  while (x != root && colorOf(x) == BLACK) {
    if (x == leftOf(parentOf(x))) {
      Entry<K,V> sib = rightOf(parentOf(x));
      if (colorOf(sib) == RED) {
        setColor(sib, BLACK);
        setColor(parentOf(x), RED);
        rotateLeft(parentOf(x));
        sib = rightOf(parentOf(x));
      }
      if (colorOf(leftOf(sib))  == BLACK &&
          colorOf(rightOf(sib)) == BLACK) {
        setColor(sib, RED);
        x = parentOf(x);
      } else {
        if (colorOf(rightOf(sib)) == BLACK) {
          setColor(leftOf(sib), BLACK);
          setColor(sib, RED);
          rotateRight(sib);
          sib = rightOf(parentOf(x));
        }
        setColor(sib, colorOf(parentOf(x)));
        setColor(parentOf(x), BLACK);
        setColor(rightOf(sib), BLACK);
        rotateLeft(parentOf(x));
        x = root;
      }
    } else { // symmetric
      Entry<K,V> sib = leftOf(parentOf(x));
      if (colorOf(sib) == RED) {
        setColor(sib, BLACK);
        setColor(parentOf(x), RED);
        rotateRight(parentOf(x));
        sib = leftOf(parentOf(x));
      }
      if (colorOf(rightOf(sib)) == BLACK &&
          colorOf(leftOf(sib)) == BLACK) {
        setColor(sib, RED);
        x = parentOf(x);
      } else {
        if (colorOf(leftOf(sib)) == BLACK) {
          setColor(rightOf(sib), BLACK);
          setColor(sib, RED);
          rotateLeft(sib);
          sib = leftOf(parentOf(x));
        }
        setColor(sib, colorOf(parentOf(x)));
        setColor(parentOf(x), BLACK);
        setColor(leftOf(sib), BLACK);
        rotateRight(parentOf(x));
        x = root;
      }
    }
  }
  setColor(x, BLACK);
}

以上就是java基礎之TreeMap實現(xiàn)類全面詳解的詳細內容,更多關于java TreeMap實現(xiàn)類的資料請關注腳本之家其它相關文章!

相關文章

  • Java實現(xiàn)并查集

    Java實現(xiàn)并查集

    這篇文章主要為大家詳細介紹了Java實現(xiàn)并查集,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Java源碼解析HashMap的tableSizeFor函數(shù)

    Java源碼解析HashMap的tableSizeFor函數(shù)

    今天小編就為大家分享一篇關于Java源碼解析HashMap的tableSizeFor函數(shù),小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • java 實現(xiàn)下壓棧的操作(能動態(tài)調整數(shù)組大小)

    java 實現(xiàn)下壓棧的操作(能動態(tài)調整數(shù)組大小)

    這篇文章主要介紹了java 實現(xiàn)下壓棧的操作(能動態(tài)調整數(shù)組大小),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 關于Java中的klass和class

    關于Java中的klass和class

    這篇文章主要介紹了關于Java中klass和class的區(qū)別,vm加載的字節(jié)碼,也就是.class文件,被加載到方法區(qū)里面,叫Kclass,是一個C++對象,含有類的信息、虛方法表等,需要的朋友可以參考下
    2023-08-08
  • JVM的垃圾處理機制詳解

    JVM的垃圾處理機制詳解

    JVM通過可達性分析確定垃圾對象,采用分代收集策略:新生代用復制算法(Eden+Survivor),老年代用標記-整理,現(xiàn)代收集器如G1兼顧吞吐與延遲,ZGC/Shenandoah實現(xiàn)超低延遲,適用于不同場景
    2025-09-09
  • java實現(xiàn)表格數(shù)據(jù)的存儲

    java實現(xiàn)表格數(shù)據(jù)的存儲

    這篇文章主要為大家詳細介紹了java實現(xiàn)表格數(shù)據(jù)的存儲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Intellij idea 代碼提示忽略字母大小寫和常用快捷鍵及設置步驟

    Intellij idea 代碼提示忽略字母大小寫和常用快捷鍵及設置步驟

    這篇文章主要介紹了Intellij idea 代碼提示忽略字母大小寫和常用快捷鍵及設置步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • Java對象創(chuàng)建的過程流程分析

    Java對象創(chuàng)建的過程流程分析

    本文詳細介紹了Java對象創(chuàng)建的整個過程,包括類加載、內存分配、對象頭設置、構造器初始化以及內存分配的安全問題,文章還討論了新生代和老年代的內存分配策略,特別是使用指針碰撞和空閑列表兩種方式,感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • SpringBoot接口防重復提交的三種解決方案

    SpringBoot接口防重復提交的三種解決方案

    在Web開發(fā)中,防止用戶重復提交表單是一個常見的需求,用戶可能會因為網絡延遲、誤操作等原因多次點擊提交按鈕,導致后臺接收到多個相同的請求,本文將介紹幾種在Spring Boot中實現(xiàn)接口防重復提交的方法,需要的朋友可以參考下
    2024-11-11
  • java?System類和Arrays類詳解

    java?System類和Arrays類詳解

    這篇文章主要介紹了java?System類和Arrays類詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08

最新評論

全椒县| 霍邱县| 肥乡县| 始兴县| 武定县| 兰州市| 玉龙| 客服| 微博| 林周县| 自治县| 谢通门县| 保亭| 万山特区| 神木县| 郁南县| 泰和县| 深泽县| 浦北县| 浙江省| 陆良县| 乌拉特中旗| 北碚区| 车险| 当阳市| 孟连| 灵山县| 尉犁县| 盘锦市| 宁陵县| 丰原市| 郧西县| 黔江区| 米脂县| 左云县| 讷河市| 上高县| 白朗县| 泰和县| 焦作市| 扎囊县|