Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the root node from a TreeMap in java?

Tags:

java

map

I have a class that extends java.util.TreeMap, I was wondering if anyone knows how I can get the root node, and for that manner how I can manually traverse the tree if I want to. When I look at the source for treemap at http://javasourcecode.org/html/open-source/jdk/jdk-6u23/java/util/TreeMap.java.html the root node is private, thus I can't neccessarily access it directly, however perhaps there is a way to access it using a combination of the methods already in treemap?

Please don't write any code for me, just point me in the right direction/pseudocode is ok.

like image 351
user1377392 Avatar asked Oct 07 '12 18:10

user1377392


People also ask

How do Treemaps work in Java?

TreeMap stores key-value pairs. The main difference is that TreeMap sorts the key in ascending order. TreeMap is sorted as the ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. TreeMap(Map map) It creates a TreeMap with the entries from a map.

Does TreeMap use hashCode?

hashCode and equals method are not required for TreeSet and TreeMap as the sorting depends on either the compareTo or compare method as been provided by the client.

Is Java TreeMap a binary search tree?

In Java, a TreeMap is a red-black tree, which is a self-balancing binary search tree.


1 Answers

You can access private members using reflection. That defeats the purpose of encapsulation in OO languages though. You can learn about reflection in this tutorial from oracle.

like image 50
Kim Stebel Avatar answered Nov 09 '22 22:11

Kim Stebel