How to create a TreeMap object in Java?
Learn the ways to create an object of TreeMap collection in Java
Introduction
In this story, 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. There are many methods that can used on the TreeMap collection. Some of them are:
- put()
- remove()
- containsKey()
- clone()
- containsValue()
- clear()
and many more!!
Let us now start exploring the different ways in which we can create an object of the TreeMap collection in Java.
Creation
There are three constructors or ways to create an object of the TreeMap class. Let’s discuss them one by one.
Using TreeMap()
In Java, the new keyword is used to create an object of any class. We construct a TreeMap object using the new keyword and specify the type of the key-value for the i.
Code
Let’s look at the code below:
Explanation
Let’s discuss the second approach now.
Using TreeMap(Comparator c)
It constructs a new and empty TreeMap object that needs an external specification to sort the elements of the TreeMap. We need to create a class that will implement the Comparator interface to perofrm the sorting of the elements based on a custom logic.
Code
Let’s look at the code below:
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.
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. It means that if the keys are numeric, then the sorting will be done based on increasing order, and if the keys are strings, then the sorting will be done based on the alphabetical order.
Code
Let’s look at the code below:
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 story, we discussed three different ways to create TreeMap in Java.
Comments
There are no comments for this story
Be the first to respond and start the conversation.