TreeMap descendingKeySet() method in java

Let’s learn TreeMap descendingKeySet() method in java.

TreeMap descendingKeySet() method in java

descendingKeySet() method of TreeMap class returns a reverse order NavigableSet view of the keys contained in this map.

The set’s iterator returns the keys in descending order.

Syntax:

public NavigableSet<K> descendingKeySet()

Parameters:

descendingKeySet() method does not accept any parameters.

Also read – TreeMap in java

Now let’s see example on TreeMap descendingKeySet() method.

import java.util.NavigableSet;
import java.util.TreeMap;
public class TreeMapDescendingKeySetMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(63, "banana");
      tm.put(56, "watermelon");
      tm.put(35, "orange");
      tm.put(83, "apple");
      tm.put(65, "pineapple");
      // put values in NavigableSet
      NavigableSet ns = tm.descendingKeySet();
      System.out.println("TreeMap values using descendingKeySet() method: ");
      System.out.println("NavigableSet values: " + ns);
   }
}


Output:

TreeMap values using descendingKeySet() method:
NavigableSet values: [83, 65, 63, 56, 35]