descendingMap() method

descendingMap(): It returns a NavigableSet view of the Key-Value(pairs) in reverse order. This method is used to fetch the Set view of all pairs(keys-values) in reverse order. It returns all the keys and values 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.

NavigableMap<K, V> descendingMap()

Where, K represents the type Keys in TreeMap and V represents the type Values 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.descendingMap());

Output: Keys and values set of TreeMap = {3=RAVI, 2=GOAL, 1=JAVA}

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.