TreeMap internal working - Java

TreeMap is a Red-Black tree based NavigableMap implementation.In other words , it sorts the TreeMap object keys using Red-Black tree algorithm.

--------------------------------------------------------------------------------------------------


TreeMap in Java is a SortedMap and it maintains Sorting order when you insert object on it.
You can specify Sorting order while Creating TreeMap by providing an explicit Comparator to
TreeMap. Basically you can create TreeMap in Java by different ways, a TreeMap with natural sorting order, and TreeMap with custom Sorting Order by providing Comparator, Copying Sorting order from other SortedMap etc. TreeMap has specific constructor for these conditions. We will see these in section of creating instance of TreeMap in Java. 


Here are few ways to create TreeMap in Java:

TreeMap in Java using natural ordering of keys

TreeMap naturalOrderMap = new TreeMap();

TreeMap with custom sorting order

TreeMap customSortingMap = new TreeMap(Comparator comparator)


Read more: http://javarevisited.blogspot.com/2011/12/treemap-java-tutorial-example-program.html#ixzz3ntDxBgYd

Comments

Popular posts from this blog

EJB - Stateful vs Stateless

Mirror binay tree - Java