HashTable Set keySet() Method Example Program


Returns a Set view of the keys contained in this map. 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.Hashtable;
import java.util.Set;

/**
 * @author: Hari Haran
 * @discription: The keySet() method is used to get a Set view of the keys
 *               contained in this Hashtable.
 */

public class HashTableKeySet {
	public static void main(String args[]) {
		Hashtable<Integer, String> ht1 = new Hashtable<Integer, String>();

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

		Set st = ht1.keySet();

		System.out.println("Display result: " + st);
	}
}

Output

Display result: [7, 6, 5, 4, 3, 2, 1]

Explanation

public Set<K> keySet()
Returns a Set view of the keys contained in this map. 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>
Returns:
a set view of the keys contained in this map


Related Post

Comments


©candidjava.com