TreeMap Tutorial In Java With Example

TreeMap is a type of Collection that stores values based on the corresponding key. This key value pair is often called as Entry. It implements the NavigableMap interface and extends AbstractMap class.

It can  stores only unique elements, that is duplicate values are not allowed and it cannot store key as null but is can  store null values. It is mostly similar to HashMap and key difference is it maintains an increasing order according to the key value.


Hierarchy of TreeMap class

TreeMap Class extends AbstractMap Class and that Implements Map Interface, TreeMap also implements NavigableMap interface, which extends Sorted Map Interface, and it further extends Map Interface as shown in fiqure 1.

Dotted arrow shows AbstractMap class implements Map Interface , and TreeMap class implements NavigableMap Interface

Bold Array shows NavigableMap Interface extends SortedMap Interface which further extends Map Interface.

Hierarchy of TreeMap class


Example of TreeMap class

Let us discuss Tree map with the help of program, following program has been divided into 4 Steps that we will discuss one by one.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
//Step 1:
TreeMap<Integer,String> treemap=new TreeMap<Integer,String>();
//Step 2:
treemap.put(102,"Core");

treemap.put(100,"I");

treemap.put(103,"Java");

treemap.put(101,"Love");

//Step 3:
for(Map.Entry map  : treemap.entrySet()){  
//Step 4:
System.out.println(map.getKey()+" "+map.getValue()); 

}

}

}
  • In Step 1, we have created an object of Tree Map collection, We have defined key of Integer type and value of String type.
  • In Step 2, we have used put method to add key value pair in the data structure that we have created in step 1.
  • In Step 3, we have used For Each loop to retrieve the values from the Tree Map. We have used method entrySet() which returns the Set containing all the keys and values
  • In Step 4 we have used two methods , First is getKey () , which as name suggest retrieves the key and secondly getValue () method that retrieves the values corresponding to the keys.

Output:

100 I

101 Love

102 Core

103 Java

In output,We have obtained output in ascending order based on the Key, which distinguishes it from hash map


TreeMap Methods In JAVA

Let us discuss TreeMap methods one by one with Examples in Java.

1 . Object  ceilingEntry(Object key)

This method returns a Entry with the  least key greater than or equal to the given key in the argument list, or null if there is no such key, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");
 
System.out.println("Ceiling entry for 99: "+ tm.ceilingEntry(99));

System.out.println("Ceiling entry for 103: "+ tm.ceilingEntry(103));

}

}

Output:

Ceiling entry for 99: 100=I

Ceiling entry for 103: 103=Java

In the output we see ceiling value of key “99” is “100” as we see 99 is not present in treemap, it automatically tool least greater than “99” which is “100”. And Ceiling value of “103” is “103”, which we see is present in the given map.

2. Object ceilingKey(Object key)

This method returns a key with the  least key greater than or equal to the given key in the argument list, or null if there is no such key, This method is same as ceilingEntry(Object key) , only difference is it returns only key, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Ceiling key for 99: "+ tm.ceilingKey(99));

System.out.println("Ceiling key for 103: "+ tm.ceilingKey(103));

}

}

Output:

Ceiling key for 99: 100

Ceiling key for 103: 103

3. void clear()

This method removes all the entries from the TreeMap object as shown

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("TreeMap before : " + tm);

tm.clear();

System.out.println("TreeMap After clear method: " + tm);

}

}

Output:

TreeMap before : {100=I, 101=Love, 102=Core, 103=Java}

TreeMap After clear method: {}

4. Object clone()

This method returns a duplicate copy of the TreeMap object, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm1 =new TreeMap<Integer,String>();

TreeMap<Integer, String> tm2 = new TreeMap<Integer, String>();

tm1.put(102,"Core");

tm1.put(100,"I");

tm1.put(103,"Java");

tm1.put(101,"Love");

//using clone method
tm2 =(TreeMap) tm1.clone();

System.out.println("Before using Clone method: "+ tm1);

System.out.println("After using Clone method: "+ tm2);

}

}

Output:

Cloning tree map

Before using Clone method: {100=I, 101=Love, 102=Core, 103=Java}

After using Clone method: {100=I, 101=Love, 102=Core, 103=Java}

Read All TreeMap Methods in JAVA with Example


Important Points About TreeMap

  • Tree Map only stores Unique values, that is duplicate values are not allowed.
  • Tree Map maintains Ascending order corresponding to keys, which means it returns the key value pair in the increasing order, irrespective of order in which they are added.
  • Tree Map does not allows null key but it can have null value in it
  • Tree Map is very much similar to Hash Map, both are not thread safe and both are unsynchronized

TreeMap Quick Revision Points

  • Data is stores in key value pair
  • Only unique values are allowed
  • insertion order is Ascending order based on key
  • It cannot have null key but can have multiple null values.
  • It is not thread safe
  • It is unsynchronized

Leave a Reply

Your email address will not be published. Required fields are marked *