TreeMap 源码解析

本文详细解析了TreeMap的数据结构,重点介绍了其基于红黑树的特性,强调了key不能为null且节点有序的特点。内容包括TreeMap的简介、重要属性与结构,特别是get查询方法和put插入操作的原理。
摘要由CSDN通过智能技术生成

TreeMap

简介

  1. 红黑树结构
  2. key 不能为空
  3. 节点是有序的,迭代顺序按照key值有序,可通过比较器指定排序规则
  4. 非线程安全

重要属性和结构

/**
     * 比较器指定节点排序规则,如果为空则通过key对象默认排序实现,如Key 为Integer 类型则是通过Integer内部实现的compareTo 方法进行比较
     */
private final Comparator<? super K> comparator;
/**
 * 根节点
 */
private transient Entry<K,V> root;

/**
     * 树中元素个数
     */
private transient int size = 0;

/**
     * treeMap 修改次数
     */
private transient int modCount = 0;

// 内部类,定义红黑树节点
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; 

    /**
         * Make a new cell with given key, value, and parent, and with
         * {@code null} child links, and BLACK color.
         */
    Entry(K key, V value, Entry<K,V> parent) {
   
        this.key = key;
        this.value = value;
        this.parent = parent;
    }


    public K getKey() {
   
        return key;
    }


    public V getValue() 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值