[18]-容器(集合)_Map_TreeMap的使用和底层结构

TreeMap
key 唯一 有序(升序)
底层数据结构 : 红黑树
使用TreeMap存储自定义数据时需要定义比较器

import java.util.Comparator;
import java.util.TreeMap;


public class TestTreeMap {
  /***
   * TreeMap的底层实现
   * 
   *  private final Comparator<? super K> comparator;  外部比较器,用于比较大小的
   *  private transient Entry<K,V> root;   //代表的是树根
   * public TreeMap() {
        comparator = null;
       }
       public V put(K key, V value) {
        Entry<K,V> t = root;   //指向树根
        if (t == null) {
              //比较大小
            compare(key, key); // type (and possibly null) check
            //创建一个根节点
            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;   //null
         //如果外部比较器不等于null,说明外部比较器存在
        if (cpr != null) {
            do {
                parent = t;   //把root赋给父节点
                
                cmp = cpr.compare(key, t.key);  //调用外部比较器的比较方法开始比大小,结果是一个int类型的值
                
                if (cmp < 0)
                    t = t.left;   //在左子树上查找
                else if (cmp > 0)   //在右子树上查找
                    t = t.right;
                else
                    return t.setValue(value);   //找到了,值进行覆盖
            } while (t != null);
        }
        else {  //外部比较器不存在,使用内部比较器进行比较
            if (key == null)
                throw new NullPointerException();
            @SuppressWarnings("unchecked")
                Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;    //root赋给父节点
                cmp = k.compareTo(t.key);  //调用内部比较器的比较大小的方法,比较结果为int类型
                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);
        
        if (cmp < 0)
            parent.left = e;   //添加到左子树
        else
            parent.right = e;    //添加到右子树
        fixAfterInsertion(e);
        size++;      
        modCount++;
        return null;
    }
   */
  public static void main(String[] args) {
    //创建集合对象
    //TreeMap treeMap=new TreeMap();
    //创建外部比较器对象,定义比较规则
    Comparator comLength=new CompareLength();
    TreeMap treeMap=new TreeMap(comLength);
    //添加数据
    treeMap.put("hello", 123);
    treeMap.put("world1", 456);
    treeMap.put("hello11", 789);
    treeMap.put("java", 1000);
    System.out.println("集合中元素的个数:"+treeMap.size());
    System.out.println(treeMap);
    System.out.println(treeMap.containsKey("hello")+"\t"+treeMap.containsKey("sql"));
    System.out.println(treeMap.containsValue(789)+"\t"+treeMap.containsValue(1002));
    System.out.println(treeMap.get("java"));
    
    
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值