TreeMap Set keySet() Method Example Program


Returns a Set 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;
import java.util.Set;

/**
 * @author: Hari Haran
 * @discription:ThekeySet() method is used to return a Set 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 TreeMapKeySet {
	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");

		Set set = tm.keySet();

		System.out.println("Checking values of the set");
		System.out.println("Value is: " + set);
	}
}

Output

Checking values of the set
Value is: [1, 2, 4, 5, 6, 7, 8]

Explanation

public Set<K> keySet()
Returns a Set 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:
keySet in interface Map<K,V>
Specified by:
keySet in interface SortedMap<K,V>
Overrides:
keySet in class AbstractMap<K,V>
Returns:
a set view of the keys contained in this map


Related Post

Comments


©candidjava.com