Open In App

How to Create TreeMap Objects using Comparable Interface in Java?

Last Updated : 22 Feb, 2023
Comments
Improve
Suggest changes
4 Likes
Like
Report

In Java, the TreeMap class is an implementation of the SortedMap interface that stores key-value pairs in a sorted order based on the natural ordering of the keys. By default, the keys are sorted in ascending order. If you want to sort the keys based on a custom ordering criteria, you can use the Comparable interface to define the natural ordering of the keys.

Here's an example of how to create a TreeMap object using the Comparable interface in Java:


Output
1 Alice value2
2 John value1
3 Bob value3

In this example, we have created a Person class that implements the Comparable interface and overrides the compareTo() method to define the natural ordering of the objects based on the id field. Then, we have created a TreeMap object using the Person class as the key and a String as the value. We have added some key-value pairs to the TreeMap and iterated over the entries to print the key-value pairs in sorted order based on the id field.

Note that if the keys are not unique, the TreeMap will only store the last value associated with the key. If you want to store multiple values for a key, you can use a Map implementation that supports multiple values per key, such as a HashMap with a List as the value.

Comparable interface is found in java.lang package which is used to order the objects of a user-defined class on the basis of a single attribute only. This interface contains a single method compareTo(Object). It is used to compare the current object with the specified objects and can be used o sort user-defined classes, wrapper class, and string objects.

Syntax: 

compareTo(Object o) ;

Parameter: The specified object with which the current object is being compared.

Return Type: Integer value

  • Positive integer — If the current object is greater than the specified object.
  • Negative integer — If the current object is lesser than the specified object.
  • Zero — If the current object is equal to the specified object.

Use of Comparable Interface in Java

TreeMap in Java, elements are stored as key-value pairs which are sorted on the basis of the key. When the Key is of String class or Wrapper Classes, it implements the Comparable interface by default and stores the elements in sorted order. However, if somebody wants to make the desired key of a particular user-defined type i.e user-defined class, we need to implement the Comparable interface to order the objects in a particular order on the basis of an attribute.

Implementation: Without using the comparable interface.

Output: Error

The above code throws exceptions as the comparable interface has not been implemented and hence it cannot order the map elements on the basis of a key in proper order. So, in order to handle the exception

Implementation: With using the comparable interface.

 
 


Output
1 Pathak First
2 Anshu Second


 


Next Article
Practice Tags :

Similar Reads