TreeMap NavigableSet descendingKeySet() Method Example Program


Returns a reverse order NavigableSet view of the keys contained in this map. The set's iterator returns the keys in descending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Program

package com.candidjava;

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

/**
 * @author: Hari Haran
 * @discription: The descendingKeySet() method is used to return a reverse order
 *               NavigableSet view of the keys contained in this map. The set's
 *               iterator returns the keys in descending order. The set is
 *               backed by the map, so changes to the map are reflected in the
 *               set, and vice-versa.
 */

public class TreeMapDescendingKey {
	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");

		NavigableSet nset = tm.descendingKeySet();

		System.out.println("Checking value");
		System.out.println("Navigable set values: " + nset);
	}
}

Output

Checking value
Navigable set values: [8, 7, 6, 5, 4, 2, 1]

Explanation

public NavigableSet<K> descendingKeySet()
Returns a reverse order NavigableSet view of the keys contained in this map. The set's iterator returns the keys in descending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
Specified by:
descendingKeySet in interface NavigableMap<K,V>
Returns:
a reverse order navigable set view of the keys in this map


Related Post

Comments


©candidjava.com