Java TreeMap descendingKeySet() Method

The TreeMap.descendingKeySet() method in Java is used to return a reverse-order view of the keys contained in the map. We will also cover a real-world use case to show how TreeMap.descendingKeySet() can be used effectively.

Table of Contents

  1. Introduction
  2. descendingKeySet Method Syntax
  3. Examples
    • Retrieving a Descending Key Set from a TreeMap
    • Iterating Over the Descending Key Set
  4. Real-World Use Case
    • Example: Displaying Student Names in Descending Order
  5. Conclusion

Introduction

The TreeMap.descendingKeySet() method is a member of the TreeMap class in Java. It returns a NavigableSet view of the keys contained in this map, ordered in reverse. This method can be useful when you need to process the keys in descending order.

descendingKeySet() Method Syntax

The syntax for the descendingKeySet method is as follows:

public NavigableSet<K> descendingKeySet()
  • The method does not take any parameters.
  • The method returns a NavigableSet<K> view of the keys contained in this map, ordered in reverse.

Examples

Retrieving a Descending Key Set from a TreeMap

The descendingKeySet method can be used to get a reverse order view of the keys in the TreeMap.

Example

import java.util.TreeMap;
import java.util.NavigableSet;

public class DescendingKeySetExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Retrieving the descending key set
        NavigableSet<String> descendingKeySet = treeMap.descendingKeySet();

        // Printing the descending key set
        System.out.println("Descending Key Set: " + descendingKeySet);
    }
}

Output:

Descending Key Set: [Vijay, Suresh, Ravi, Priya, Anita]

Iterating Over the Descending Key Set

You can also iterate over the keys in the descending key set.

Example

import java.util.TreeMap;
import java.util.NavigableSet;

public class IterateDescendingKeySetExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Retrieving the descending key set
        NavigableSet<String> descendingKeySet = treeMap.descendingKeySet();

        // Iterating over the descending key set
        System.out.println("Iterating over the descending key set:");
        for (String key : descendingKeySet) {
            System.out.println(key);
        }
    }
}

Output:

Iterating over the descending key set:
Vijay
Suresh
Ravi
Priya
Anita

Real-World Use Case

Example: Displaying Student Names in Descending Order

A common real-world use case for TreeMap.descendingKeySet() is displaying student names in descending order.

Example

import java.util.TreeMap;
import java.util.NavigableSet;

public class StudentNamesManager {
    public static void main(String[] args) {
        // Creating a TreeMap to manage student names and their IDs
        TreeMap<String, Integer> studentNames = new TreeMap<>();

        // Adding student names and their IDs to the TreeMap
        studentNames.put("Ravi", 101);
        studentNames.put("Priya", 102);
        studentNames.put("Vijay", 103);
        studentNames.put("Anita", 104);
        studentNames.put("Suresh", 105);

        // Retrieving the descending key set
        NavigableSet<String> descendingNames = studentNames.descendingKeySet();

        // Printing the student names in descending order
        System.out.println("Student names in descending order:");
        for (String name : descendingNames) {
            System.out.println(name);
        }
    }
}

Output:

Student names in descending order:
Vijay
Suresh
Ravi
Priya
Anita

In this example, TreeMap.descendingKeySet() is used to display student names in descending order, making it easy to view the names from highest to lowest.

Conclusion

The TreeMap.descendingKeySet() method in Java provides a way to retrieve a reverse-order view of the keys contained in the map. By understanding how to use this method, you can efficiently manage and navigate collections of keys in your Java applications. The method allows you to work with the keys in descending order, making it a versatile tool for data management in various scenarios.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top