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
</