TreeMap NavigableSet navigableKeySet() Method Example Program


Returns a NavigableSet view of the keys contained in this map. The set's iterator returns the keys in ascending 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;

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

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

		System.out.println("Checking key set value");
		System.out.println("Value is: " + tm.navigableKeySet());
	}
}

Output

Checking key set value
Value is: [1, 2, 4, 5, 6, 7, 8]

Explanation

public NavigableSet<K> navigableKeySet()
Returns a NavigableSet view of the keys contained in this map. The set's iterator returns the keys in ascending 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:
navigableKeySet in interface NavigableMap<K,V>
Returns:
a navigable set view of the keys in this map


Related Post

Comments


©candidjava.com