TreeMap Class In Java

The TreeMap class in Java implements the Map interface, extending the abstract map class. The elements are sorted according to the natural sorting order or by the comparator provided during TreeMap creation. The treeMap class is not threaded safely or synchronized.

Key points about TreeMap

  • It Stores elements on the basis of key and value pairs.
  • This class implements the Map interface, including NavigableMap, SortedMap and, extends AbstractMap.
  • It does not allow duplicate values.
  • It does not allow null keys, but you can insert multiple null values.
  • It stores the keys in sorted (natural order) or by a Comparator provided at map creation time.
  • This class is not threaded-safe, but we can make it thread-safe using Collections.synchronizedSortedMap(new TreeMap()) for working in a multi-threading environment.

Example:

SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));

Constructors in TreeMap Class

  • TreeMap(): Constructs an empty treemap that will be sorted using its keys’ natural order.
  • TreeMap(Comparator comp): Constructs an empty tree-based map that will be sorted using the Comparator comp.
  • TreeMap(Map m): Initializes a treemap with the entries from m, which will be sorted by using the natural order of the keys.
  • TreeMap(SortedMap sm): Initializes a treemap with the entries from sm, which will be sorted in the same order as sm.

How do you find a Treemap Value ContainsValue() in Java with an example?

package com.java.Softwaretestingblog;
import java.util.TreeMap;
public class TreeMapValueSearch {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      TreeMap<String, String> hm = new TreeMap<String, String>();
      //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");
      }
   }
}

Output:

{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
The TreeMap contains value SECOND INSERTED
The TreeMap does not contain value first

How to Use Treemap Different Methods With Java Example?

package com.java.Softwaretestingblog;
import java.util.TreeMap;
public class Treemap_Different_Methods {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      TreeMap<String, String> hm = new TreeMap<String, String>();
      //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
      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());
   }
}

Output:

{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
Value of second: SECOND INSERTED
Is TreeMap empty? false
{first=FIRST INSERTED, second=SECOND INSERTED}
Size of the TreeMap: 2

How do you remove Treemap Duplicate Values From Array Using Java?

package com.java.Softwaretestingblog;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
public class Remove_Treemap_Duplicate_Values {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      String[] strArr = {"one","two","three","four","four","five"};
      //convert string array to list
      List<String> tmpList = Arrays.asList(strArr);
      //create a treeset with the list, which eliminates duplicates
      TreeSet<String> unique = new TreeSet<String>(tmpList);
      System.out.println(unique);
   }
}

Output:

[five, four, one, three, two]

How do you retrieve treemap values using a key in Java with an example?

package com.java.Softwaretestingblog;
import java.util.Set;
import java.util.TreeMap;
public class Retrive_Treemap_Value_Using_Key {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      TreeMap<String, String> hm = new TreeMap<String, String>(); 

      //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<String> keys = hm.keySet(); 
      for(String key: keys)
      {
         System.out.println("Value of "+key+" is: "+hm.get(key)); 
      }
   }
}

Output:

{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
Value of first is: FIRST INSERTED
Value of second is: SECOND INSERTED
Value of third is: THIRD INSERTED

How do you clear TreeMap Values in Java with a real-time example?

package com.java.Softwaretestingblog;
import java.util.TreeMap;
public class ClearTreemapValues {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      TreeMap<String, String> hm = new TreeMap<String, String>();
        //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);
   }
}

Output:

My TreeMap content:
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
Clearing TreeMap:
Content After clear:
{}

How do you find a Treemap Value containsKey() in Java with an example?

package com.java.Softwaretestingblog;
import java.util.TreeMap;
public class Treemap_Value_containsKey {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      TreeMap<String, String> hm = new TreeMap<String, String>();

      //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.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");
      }
   }
}

Output:

{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
The TreeMap contains key first
The TreeMap does not contain key fifth

Retrieve the TreeMap Values Java Program: How to Add & Retrieve TreeMap Values Using Java With Example?

package com.java.Softwaretestingblog;
import java.util.Set;
import java.util.TreeMap;
public class ReadTreemapValues {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      TreeMap<String, String> hm = new TreeMap<String, String>();
      //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<String> keys = hm.keySet();
      for(String key: keys)
      {
         System.out.println(key);
      }
   }
}

Output:

{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
first
second
third

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment