TreeMap in Java

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

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.

Output:

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.
tmap.get(“42”); 
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.
map.containsValue(“Test2”);

 

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.
tmap.containsKey(“42”);

 

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.
tmap.firstEntry();
tmap.firstKey();

 

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.
tmap.lastEntry();
tmap.lastKey();

 

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