Using CompareTo method in TreeMap in Java Collections
Table of Contents
- Introduction
- TreeMap and CompareTo Method Overview
- Code Example and Explanation
- Output and Analysis
- Conclusion
1. Introduction
In Java, the TreeMap class provides an efficient way to store key-value pairs in a sorted order. One of the core features that enable this sorting is the compareTo()
method, which is part of the Comparable
interface. This method allows for custom sorting of the keys, making TreeMap a flexible data structure for many applications.
In this article, we will explore how the compareTo()
method works within the TreeMap context, specifically focusing on the provided Java program that implements a custom Comparable
interface. We will discuss the importance of the compareTo()
method, how it can be utilized, and walk through a detailed example to showcase its functionality.
2. TreeMap and CompareTo Method Overview
The TreeMap class is a part of Java’s Collection Framework. It is a Red-Black tree-based implementation of the NavigableMap
interface. The entries in a TreeMap are sorted based on the keys, either using the natural order of the keys or by a custom comparator provided at map creation time.
The compareTo()
method, on the other hand, is crucial for defining the natural ordering of objects. The method compares the current object with the specified object for order. It returns:
- A negative integer if the current object is less than the specified object.
- Zero if they are equal.
- A positive integer if the current object is greater than the specified object.
3. Code Example and Explanation
The following Java program demonstrates the use of compareTo()
within the context of a TreeMap
.
Main Components of the Program
The Code
class implements the Comparable<Code>
interface, meaning that the objects of this class can be compared to each other. The class contains two string attributes, sectionNo
and lectureNo
, which represent different sections and lectures in an educational context.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Code implements Comparable<Code> { private String sectionNo; private String lectureNo; public Code(String sectionNo, String lectureNo) { this.sectionNo = sectionNo; this.lectureNo = lectureNo; } @Override public int compareTo(Code o) { String code1 = sectionNo.concat(lectureNo); String code2 = o.getSectionNo().concat(o.getLectureNo()); return code1.compareTo(code2); } } |
Implementation of Comparable in TreeMap
The compareTo()
method compares the sectionNo
and lectureNo
attributes of two Code
objects. It concatenates the two attributes into a single string for each object and then compares these strings lexicographically using the compareTo()
method of the String
class.
The TreeMap
uses the compareTo()
method to keep the keys sorted. Here’s the main code that demonstrates the usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { Map<Code, String> map = new TreeMap<>(); map.put(new Code("S11", "11"), "Chaand"); map.put(new Code("S11", "22"), "Rahul"); map.put(new Code("S11", "00"), "Alex"); map.put(new Code("S11", "05"), "John"); map.put(new Code("S10", "10"), "Aafiya"); map.put(new Code("S11", "10"), "Chaand"); System.out.println(map); } } |
4. Output and Analysis
Output:
1 2 3 |
{Code{sectionNo='S10', lectureNo='10'}=Aafiya, Code{sectionNo='S11', lectureNo='00'}=Alex, Code{sectionNo='S11', lectureNo='05'}=John, Code{sectionNo='S11', lectureNo='10'}=Chaand, Code{sectionNo='S11', lectureNo='11'}=Chaand, Code{sectionNo='S11', lectureNo='22'}=Rahul} |
Analysis:
The TreeMap sorts the keys (instances of Code
) based on the result of the compareTo()
method. In this case, the Code
objects are sorted in ascending order, first by sectionNo
, and then by lectureNo
. We see that the map entries are printed in the order dictated by the compareTo()
method.
Here, the object with the lowest sectionNo
and lectureNo
comes first, and the one with the highest comes last.
5. Conclusion
In this article, we explored the workings of the TreeMap class and the compareTo()
method from the Comparable
interface in Java. By implementing a custom compareTo()
method, we can define the sorting order of the keys within the TreeMap. This flexibility makes TreeMap a powerful tool for scenarios where we need to maintain a sorted order of custom objects.