TreeMap NavigableMap descendingMap() Method Example Program


Returns a reverse order view of the mappings contained in this map. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa. If either map is modified while an iteration over a collection view of either map is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.

Program

package com.candidjava;

import java.util.TreeMap;
import java.util.NavigableMap;

/**
 * @author: Hari Haran
 * @discription: The descendingMap() method is used to return a reverse order
 *               view of the mappings contained in this map. The descending map
 *               is backed by this map, so changes to the map are reflected in
 *               the descending map, and vice-versa.
 */

public class TreeMapDescendingMap {
	public static void main(String[] args) {
		TreeMap<Integer, String> tm = new TreeMap<Integer, String>();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");

		NavigableMap<Integer, String> nmap = tm.descendingMap();

		System.out.println("Checking value");
		System.out.println("Navigable map values: " + nmap);
	}
}

Output

Checking value
Navigable map values: {8=Kamal, 7=Hari Haran, 6=Vinoth, 5=Mohan, 4=Karthikeyan, 2=Hari Haran, 1=Anand}

Explanation

public NavigableMap<K,V> descendingMap()
Returns a reverse order view of the mappings contained in this map. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa. If either map is modified while an iteration over a collection view of either map is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.
The returned map has an ordering equivalent to Collections.reverseOrder(comparator()). The expression m.descendingMap().descendingMap() returns a view of m essentially equivalent to m.

Specified by:
descendingMap in interface NavigableMap<K,V>
Returns:
a reverse order view of this map


Related Post

Comments


©candidjava.com