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

/* 
 * @author : Mohan 
 * @description : The keySet() method is used to get a Set view of the keys contained in this Hashtable.
 */
public class PropertiesKeySet {

	public static void main(String[] args) {
		Properties properties = new Properties();
		Hashtable ht = new Hashtable(properties);
		ht.put("1", "hari");
		ht.put("2", "anand");
		ht.put("3", "vinoth");
		ht.put("4", "mohan");
		ht.put("5", "karthick");
		System.out.println(ht);
		Set st = ht.keySet();
		System.out.println(st);
	}

}

Output

{5=karthick, 4=mohan, 3=vinoth, 2=anand, 1=hari}
[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
Since:
1.2


Related Post

Comments


©candidjava.com