Easy Tutorial
For Competitive Exams

Java Programming TreeMap

What is TreeMap?

TreeMap class implements the Map interface by using a tree. It provides an efficient means of storing key/value pairs in sorted order.

A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.

It contains only unique elements.

It cannot have null key but can have multiple null values.

It is same as HashMap instead maintains ascending order.

How to perform Basic Operations TreeMap?

The Basic Operations are:

  • creating an object
  • adding key-value pair objects
  • getting value by passing key object
  • checking whether the map has elements or not
  • deleting specific entry
  • size of the TreeMap

Basic Operations

import java.util.TreeMap;
public class MyBasicOperations {
        public static void main(String a[]){
        TreeMap hm = new TreeMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        System.out.println("Value of second: "+hm.get("second"));
        System.out.println("Is TreeMap empty? "+hm.isEmpty());
        hm.remove("third");
        System.out.println(hm);
        System.out.println("Size of the TreeMap: "+hm.size());
    }
}
Try it Yourself

How to iterate through TreeMap?

First we can get all keys by calling keySet() method, which returns list of keys as Set object. By going through each element in the set, we can get corresponding values from TreeMap.

Example

import java.util.Set;
import java.util.TreeMap;
public class MyTreeMapRead {
  public static void main(String a[]){
        TreeMap hm = new TreeMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys){
            System.out.println("Value of "+key+" is: "+hm.get(key));
        }
    }
}
Try it Yourself

How to copy Map content to another TreeMap?

By using putAll() method, we can copy all entries from another collections to TreeMap.

Example

import java.util.TreeMap;
public class MyTreeMapCopy {
    public static void main(String a[]){
        TreeMap hm = new TreeMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        TreeMap subMap = new TreeMap();
        subMap.put("s1", "S1 VALUE");
        subMap.put("s2", "S2 VALUE");
        hm.putAll(subMap);
        System.out.println(hm);
    }
}
Try it Yourself

How to search a key in TreeMap?

By using containsKey() method we can find out keys presence in the TreeMap.

Example

import java.util.TreeMap;
public class KeySearch {
     public static void main(String a[]){
        TreeMap hm = new TreeMap();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        if(hm.containsKey("first")){
            System.out.println("The TreeMap contains key first");
        } else {
            System.out.println("The TreeMap does not contains key first");
        }
        if(hm.containsKey("fifth")){
            System.out.println("The TreeMap contains key fifth");
        } else {
            System.out.println("The TreeMap does not contains key fifth");
        }
    }
}
Try it Yourself

How to search a value in TreeMap?

By using containsValue() method we can find out value presence in the TreeMap.

Example

import java.util.TreeMap;
public class ValueSearch {
     public static void main(String a[]){
        TreeMap hm = new TreeMap();
        //add key-value pair to TreeMap
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        if(hm.containsValue("SECOND INSERTED")){
            System.out.println("The TreeMap contains value SECOND INSERTED");
        } else {
            System.out.println("The TreeMap does not contains value SECOND INSERTED");
        }
        if(hm.containsValue("first")){
            System.out.println("The TreeMap contains value first");
        } else {
            System.out.println("The TreeMap does not contains value first");
        }
    }
}
Try it Yourself

How to get all keys from TreeMap?

By using keySet() method, you can get all key entries as a Set object.

Example

import java.util.Set;
import java.util.TreeMap;
 
public class MapKeys {
 
    public static void main(String a[]){
        TreeMap hm = new TreeMap();
        //add key-value pair to TreeMap
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys){
            System.out.println(key);
        }
    }
}
Try it Yourself

How to get entry set from TreeMap?

Entry class provides getter methods to access key-value details. The method entrySet() provides all entries as set object.

Example

import java.util.Set;
import java.util.Map.Entry;
import java.util.TreeMap;
 
public class EntrySet {
     
    public static void main(String a[]){
        TreeMap hm = new TreeMap();
        //add key-value pair to TreeMap
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        //getting value for the given key from TreeMap
        Set> entires = hm.entrySet();
        for(Entry ent:entires){
            System.out.println(ent.getKey()+" ==> "+ent.getValue());
        }
    }
}
Try it Yourself

How to delete all elements from TreeMap?

By calling clear() method, we can delete all key-value pairs from TreeMap.

Example

import java.util.TreeMap;
public class MapClear {
      public static void main(String a[]){
        TreeMap hm = new TreeMap();
        //add key-value pair to TreeMap
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println("My TreeMap content:");
        System.out.println(hm);
        System.out.println("Clearing TreeMap:");
        hm.clear();
        System.out.println("Content After clear:");
        System.out.println(hm);
    }
}
Try it Yourself

Solve this!!

20848.What is the output of this program?
    import java.util.*;
    class Maps {
        public static void main(String args[]) {
            TreeMap obj = new TreeMap();
            obj.put("A", new Integer(1));
            obj.put("B", new Integer(2));
            obj.put("C", new Integer(3));
            System.out.println(obj.entrySet());
        }
    }
[A, B, C]
[1, 2, 3]
{A=1, B=2, C=3}
[A=1, B=2, C=3]
Explanation:
obj.entrySet() method is used to obtain a set that contains the entries in the map [entry1,entry2....entryn].
20849.What is the output of this program?
import java.util.*;
    class Maps {
        public static void main(String args[]) {
            TreeMap obj = new TreeMap();
            
            obj.put("B", new Integer(2));
             obj.put(null, null);
            obj.put("C", new Integer(3));
           
            System.out.println(obj.entrySet());
        }
    }
[B=2,null,C=3]
[B=2,C=3]
Error
NullPointerException
Explanation:
TreeMap doesn't allow null keys.
20850.What is the output of this program?
import java.util.*;
    class Maps {
        public static void main(String args[]) {
            TreeMap obj = new TreeMap();
            obj.put("B", null);
            obj.put("C", null);
            System.out.println(obj);
        }
    }
{B=null, C=null}
NullPointerException
[B=null,C=null]
Error
Explanation:
obj is used to obtain a set that contains the entries in the map. {entry1,entry2...entryn}.
Share with Friends