descendingKeySet() method

descending KeySet in Java: This descendingKeySet() method is used to fetch the Set view of all keys in reverse order. It returns all the key in form of NavigableSet. This Set worked on backed of TreeMap, if any change makes in TreeMap also reflects in this Set and vice versa. It returns type is Set.

NavigableSet<K> descendingKeySet();

Where, K represents the type Keys in TreeMap.

import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer,String> listOfNames = new TreeMap<Integer, String>();
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		
		System.out.println("Keys and values set of TreeMap = "+listOfNames.descendingKeySet());
		
	}
}

Output: Keys and values set of TreeMap = [3, 2, 1]

Leave a Comment