@@ -12,17 +12,31 @@ tags:
1212
1313<!-- TOC -->
1414
15- - [ Map] ( #map )
16- - [ Map 要点] ( #map-要点 )
17- - [ HashMap] ( #hashmap )
18- - [ HashMap 要点] ( #hashmap-要点 )
19- - [ HashMap 源码] ( #hashmap-源码 )
20- - [ 小结] ( #小结 )
21- - [ LinkedHashMap] ( #linkedhashmap )
22- - [ LinkedHashMap 要点] ( #linkedhashmap-要点 )
23- - [ LinkedHashMap 源码] ( #linkedhashmap-源码 )
24- - [ TreeMap] ( #treemap )
25- - [ 资料] ( #资料 )
15+ - [ 容器之 Map] ( #map )
16+ - [ Map] ( #map )
17+ - [ Map 要点] ( #map )
18+ - [ HashMap] ( #hashmap )
19+ - [ HashMap 要点] ( #hashmap )
20+ - [ HashMap 源码] ( #hashmap )
21+ - [ HashMap 定义] ( #hashmap )
22+ - [ 构造方法] ( # )
23+ - [ put 函数的实现] ( #put )
24+ - [ get 函数的实现] ( #get )
25+ - [ hash 函数的实现] ( #hash )
26+ - [ resize 的实现] ( #resize )
27+ - [ 小结] ( # )
28+ - [ LinkedHashMap] ( #linkedhashmap )
29+ - [ LinkedHashMap 要点] ( #linkedhashmap )
30+ - [ LinkedHashMap 源码] ( #linkedhashmap )
31+ - [ LinkedHashMap 定义] ( #linkedhashmap )
32+ - [ TreeMap] ( #treemap )
33+ - [ TreeMap 要点] ( #treemap )
34+ - [ TreeMap 源码] ( #treemap )
35+ - [ put 函数] ( #put )
36+ - [ get 函数] ( #get )
37+ - [ remove 函数] ( #remove )
38+ - [ TreeMap 示例] ( #treemap )
39+ - [ 资料] ( # )
2640
2741<!-- /TOC -->
2842
@@ -425,6 +439,204 @@ LinkedHashMap 继承了 HashMap 的 put 函数,本身没有实现 put 函数
425439
426440## TreeMap
427441
442+ ### TreeMap 要点
443+
444+ TreeMap 基于红黑树实现。
445+
446+ TreeMap 是有序的。它的排序规则是:根据 map 中的 key 的自然顺序或提供的比较器的比较顺序。
447+
448+ TreeMap 不是并发安全的。
449+
450+ ### TreeMap 源码
451+
452+ #### put 函数
453+
454+ ``` java
455+ public V put(K key, V value) {
456+ Entry<K ,V > t = root;
457+ // 如果根节点为 null,插入第一个节点
458+ if (t == null ) {
459+ compare(key, key); // type (and possibly null) check
460+
461+ root = new Entry<> (key, value, null );
462+ size = 1 ;
463+ modCount++ ;
464+ return null ;
465+ }
466+ int cmp;
467+ Entry<K ,V > parent;
468+ // split comparator and comparable paths
469+ Comparator<? super K > cpr = comparator;
470+ // 每个节点的左孩子节点的值小于它;右孩子节点的值大于它
471+ // 如果有比较器,使用比较器进行比较
472+ if (cpr != null ) {
473+ do {
474+ parent = t;
475+ cmp = cpr. compare(key, t. key);
476+ if (cmp < 0 )
477+ t = t. left;
478+ else if (cmp > 0 )
479+ t = t. right;
480+ else
481+ return t. setValue(value);
482+ } while (t != null );
483+ }
484+ // 没有比较器,使用 key 的自然顺序进行比较
485+ else {
486+ if (key == null )
487+ throw new NullPointerException ();
488+ @SuppressWarnings (" unchecked" )
489+ Comparable<? super K > k = (Comparable<? super K > ) key;
490+ do {
491+ parent = t;
492+ cmp = k. compareTo(t. key);
493+ if (cmp < 0 )
494+ t = t. left;
495+ else if (cmp > 0 )
496+ t = t. right;
497+ else
498+ return t. setValue(value);
499+ } while (t != null );
500+ }
501+ // 通过上面的遍历未找到 key 值,则新插入节点
502+ Entry<K ,V > e = new Entry<> (key, value, parent);
503+ if (cmp < 0 )
504+ parent. left = e;
505+ else
506+ parent. right = e;
507+ // 插入后,为了维持红黑树的平衡需要调整
508+ fixAfterInsertion(e);
509+ size++ ;
510+ modCount++ ;
511+ return null ;
512+ }
513+ ```
514+
515+ #### get 函数
516+
517+ ``` java
518+ public V get(Object key) {
519+ Entry<K ,V > p = getEntry(key);
520+ return (p== null ? null : p. value);
521+ }
522+
523+ final Entry<K ,V > getEntry(Object key) {
524+ // Offload comparator-based version for sake of performance
525+ if (comparator != null )
526+ return getEntryUsingComparator(key);
527+ if (key == null )
528+ throw new NullPointerException ();
529+ @SuppressWarnings (" unchecked" )
530+ Comparable<? super K > k = (Comparable<? super K > ) key;
531+ Entry<K ,V > p = root;
532+ // 按照二叉树搜索的方式进行搜索,搜到返回
533+ while (p != null ) {
534+ int cmp = k. compareTo(p. key);
535+ if (cmp < 0 )
536+ p = p. left;
537+ else if (cmp > 0 )
538+ p = p. right;
539+ else
540+ return p;
541+ }
542+ return null ;
543+ }
544+ ```
545+
546+ ### remove 函数
547+
548+ ``` java
549+ public V remove(Object key) {
550+ Entry<K ,V > p = getEntry(key);
551+ if (p == null )
552+ return null ;
553+
554+ V oldValue = p. value;
555+ deleteEntry(p);
556+ return oldValue;
557+ }
558+ private void deleteEntry(Entry<K ,V > p) {
559+ modCount++ ;
560+ size-- ;
561+
562+ // 如果当前节点有左右孩子节点,使用后继节点替换要删除的节点
563+ // If strictly internal, copy successor's element to p and then make p
564+ // point to successor.
565+ if (p. left != null && p. right != null ) {
566+ Entry<K ,V > s = successor(p);
567+ p. key = s. key;
568+ p. value = s. value;
569+ p = s;
570+ } // p has 2 children
571+
572+ // Start fixup at replacement node, if it exists.
573+ Entry<K ,V > replacement = (p. left != null ? p. left : p. right);
574+
575+ if (replacement != null ) {
576+ // Link replacement to parent
577+ replacement. parent = p. parent;
578+ if (p. parent == null )
579+ root = replacement;
580+ else if (p == p. parent. left)
581+ p. parent. left = replacement;
582+ else
583+ p. parent. right = replacement;
584+
585+ // Null out links so they are OK to use by fixAfterDeletion.
586+ p. left = p. right = p. parent = null ;
587+
588+ // Fix replacement
589+ if (p. color == BLACK )
590+ fixAfterDeletion(replacement);
591+ } else if (p. parent == null ) { // return if we are the only node.
592+ root = null ;
593+ } else { // No children. Use self as phantom replacement and unlink.
594+ if (p. color == BLACK )
595+ fixAfterDeletion(p);
596+
597+ if (p. parent != null ) {
598+ if (p == p. parent. left)
599+ p. parent. left = null ;
600+ else if (p == p. parent. right)
601+ p. parent. right = null ;
602+ p. parent = null ;
603+ }
604+ }
605+ }
606+ ```
607+
608+ ### TreeMap 示例
609+
610+ ``` java
611+ public class TreeMapDemo {
612+
613+ private static final String [] chars = " A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" . split(" " );
614+
615+ public static void main (String [] args ) {
616+ TreeMap<Integer , String > treeMap = new TreeMap<> ();
617+ for (int i = 0 ; i < chars. length; i++ ) {
618+ treeMap. put(i, chars[i]);
619+ }
620+ System . out. println(treeMap);
621+ Integer low = treeMap. firstKey();
622+ Integer high = treeMap. lastKey();
623+ System . out. println(low);
624+ System . out. println(high);
625+ Iterator<Integer > it = treeMap. keySet(). iterator();
626+ for (int i = 0 ; i <= 6 ; i++ ) {
627+ if (i == 3 ) { low = it. next(); }
628+ if (i == 6 ) { high = it. next(); } else { it. next(); }
629+ }
630+ System . out. println(low);
631+ System . out. println(high);
632+ System . out. println(treeMap. subMap(low, high));
633+ System . out. println(treeMap. headMap(high));
634+ System . out. println(treeMap. tailMap(low));
635+ }
636+ }
637+ ```
638+
428639## 资料
429640
430- https://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E7%8E%B0/
641+ * https://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E5%AE%9E%E7%8E%B0/
642+ * https://blog.csdn.net/justloveyou_/article/details/71713781
0 commit comments