IDEA通过反射查看TreeMap的红黑树

1.根节点所对应的属性

先通过TreeMap的源码找到根节点所代表的属性

/**
 * The comparator used to maintain order in this tree map, or
 * null if it uses the natural ordering of its keys.
 *
 * @serial
 */
private final Comparator<? super K> comparator;

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.
 */
private transient int modCount = 0;

可以看出root就是红黑树的根节点,于是我们通过属性反射获取到root属性。由于root的属性是private,我们需要设置访问权限。

//通过反射获取到root节点
Field rootField= TreeMap.class.getDeclaredField("root");
//将roo的权限设置为true
rootField.setAccessible(true);

2.根节点所对应的类类型

通过源码可以发现root所对应的类是TreeMap.Entry,是TreeMap的内部类。我们需要获取到Entry这个类型

//打印出TreeMap所有的内部类
Class<?>[] classes = TreeMap.class.getDeclaredClasses();
for(Class<?> clazz : classes){
    System.out.println(clazz.getSimpleName());
}
AscendingSubMap
DescendingKeyIterator
DescendingKeySpliterator
DescendingSubMap
Entry
EntryIterator
EntrySet
EntrySpliterator
KeyIterator
KeySet
KeySpliterator
NavigableSubMap
PrivateEntryIterator
SubMap
TreeMapSpliterator
ValueIterator
ValueSpliterator
Values
</
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值