TreeMap in Java

TreeMap in Java implements the NavigableMap interface of the Map interface.  TreeMap is Red-Black tree based NavigableMap implementation. The main difference between HashMap and TreeMap is that HashMap is an unordered collection while TreeMap is sorted in the ascending order of its keys.


TreeMap is unsynchronized collection class which means it is not suitable for thread-safe operations until unless synchronized explicitly.


Implementation of TreeMap

Let’s understand the TreeMap with a simple example. In this example, we are implementing the TreeMap Adding the value in TreeMap and performing other important operations on TreeMap.


key is: 10 & Value is: Test1
key is: 12 & Value is: Test2
key is: 42 & Value is: Test4
key is: 72 & Value is: Test5
key is: 85 & Value is: Test3


You can see that we have inserted the data in random order however when we displayed the TreeMap content we got the sorted result in the ascending order of keys.


Examples of TreeMap

Now let’s understand the other useful operations on TreeMap.

1. Retrieve the object from TreeMap – Simple just use get(key) method and provide key and it will return value from TreeMap.
As per our example code, It will return the value Test4.


2. Checking a value exists in Java TreeMap – Sometimes we want to see whether a particular value exists in TreeMap or not, this is quite easy by using utility methodcontainsValue(Object value) of TreeMap class in Java. This method returns true if TreeMap contains specified value otherwise return false.


3. How to check a key exists in TreeMap – Checking for values in treeMap in Java you can also search for keys by using method containsKey() this method will return true if you contain specified the key or return false if TreeMap doesn’t contain that key.


4. How to get first entry from TreeMap in Java – TreeMap in Java provides the convenient method to get firstKey and lastKey from TreeMap. Below is example of TreeMap and getting first key and first entry. FirstKeywill throw NoSuchElementException exception if TreeMap is empty while firstEntry will return null.


5. How to get last entry from Java TreeMap – Similar to above example of getting first entry and first key you can also get last entry and last key from treeMap in Java.


6. Creating subMap from TreeMap in Java – From JDK 1.6 onwards we have a subMap() method in TreeMap which returns portion of Map while the key range is specified by fromKey to toKey. The returned Map is backed by original TreeMap and any change made in subMap will reflect back in TreeMap and vice-versa.
SortedMap subMap = tmap.subMap(85, 72);


7. Checking whether TreeMap is empty – There is convenient isEmpty() method from AbstactMap which is used to check whether TreeMap in java is empty or not, its return true if TreeMap doesn’t contain any entry.
boolean isEmpty = tmap.isEmpty();


This is how can we implement the TreeMap and perform the different methods on TreeMap.

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply