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.
Table of Contents
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.
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()); } } }
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
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
Premium Project Source Code:
Leave a Reply