How to create a TreeMap object in Java

Introduction

In this shot, we’ll learn about the TreeMap collection, a class present in the Collections framework of Java. We generally use it to instantiate objects for Map and NavigableMap interfaces. It is a key-value pair collection, and it is sorted based on its keys.

TreeMap does not allow us to insert a null key in the map.

Creation

There are three constructors or ways to create an object of the TreeMap class. Let’s discuss them one by one.

1. Using TreeMap()

We construct a TreeMap object using the new keyword and specify the type of the key-value for the TreeMap.

Code

Let’s look at the code below:

import java.util.*;
class Main
{
public static void main(String[] args)
{
TreeMap<Integer, String> t1 = new TreeMap<Integer, String>();
t1.put(1, "Let's");
t1.put(5, "see");
t1.put(2, "TreeMap class");
t1.put(27, "methods");
t1.put(9, "in java.");
System.out.println("TreeMap using first constructor is:" + t1);
}
}

Explanation

  • Line 1: We import the required package.

  • Line 2: We make a Main class.

  • Line 6: We make the first constructor for TreeMap creation. We declare a TreeMap with Integer type keys and String type values.

  • Lines 8 to 12: We insert the entries in the map using the TreeMap.put() method.

  • Line 14: We display the TreeMap contents.

Let’s discuss the second approach now.

2. Using TreeMap(Comparator c)

It constructs a new and empty TreeMap object that needs an external specification to sort the elements of the TreeMap.

Code

Let’s look at the code below:

import java.util.*;
class Student
{
int rollno;
String name;
public Student(int rollno, String name)
{
this.rollno = rollno;
this.name = name;
}
public String toString()
{
return this.rollno + " " + this.name;
}
}
class Sortbyroll implements Comparator<Student>
{
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
class Main
{
public static void main(String[] args)
{
TreeMap<Student, Integer> t2 = new TreeMap<Student, Integer>(new Sortbyroll());
t2.put(new Student(11, "John"), 23);
t2.put(new Student(91, "Jack"), 39);
t2.put(new Student(21, "Dane"), 31);
System.out.println("TreeMap using 2nd constructor is:"+t2);
}
}

Explanation

  • Line 1: We import the required package.

  • Line 2: We make a Main class.

  • Lines 2 to 10: We create a class and initialize its data members with the help of the constructor.

  • Lines 11 to 14: We define a member function of the above class, which returns its data members.

  • Lines 17 to 23: We make another class that implements the Comparator interface and compares one of the data members of the above class. We will use this class to create a TreeMap, which sorts based on a custom comparator.

  • Line 29: We make the second constructor for TreeMap creation and sort it according to a comparator.

  • Lines 31 to 33: We insert the entries in the map using the TreeMap.put() method.

  • Line 35: We display the TreeMap content. We can see that the content is sorted based on the rollno.

Let’s now explore the third approach.

3. Using TreeMap(Map m)

It constructs a new TreeMap with the mapping initialized as in the Map m, which will be sorted using the natural order of the keys.

Code

Let’s look at the code below:

import java.util.*;
class Main
{
public static void main(String[] args)
{
HashMap<Integer, String> h = new HashMap<Integer, String>();
h.put(31, "Let's");
h.put(50, "see");
h.put(12, "Treemap with 3rd constructor");
h.put(49, "in java.");
TreeMap<Integer, String> t3 = new TreeMap<Integer, String>(h);
System.out.println("TreeMap using 3rd constructor is:"+t3);
}
}

Explanation

  • Line 1: We import the required package.

  • Line 2: We make a Main class.

  • Line 5: We declare a HashMap with Integer type keys and String type values.

  • Lines 8 to 11: We insert the entries in the map using the HashMap.put() method.

  • Line 13: We declare a TreeMap with Integer type keys and String type values and initialize all its entries as HashMap.

  • Line 15: We display the TreeMap content.

In this shot, we discussed three different ways to create TreeMap in Java.

Free Resources