Hashtable keySet() Method in Java with Examples Last Updated : 15 Oct, 2020 Comments Improve Suggest changes Like Article Like Report The java.util.Hashtable is used to create a set of key elements in the hash table. It basically returns a set view of the keys, or we can create a new set and store the key elements in them. Syntax: public Set<K> keySet() K : type of the Keys in the hash table Parameters: The method does not take any parameter. Return Value: The method returns a set having the keys of the hash table. Below programs are used to illustrate the working of java.util.Hashtable.keySet() Method:Example 1: Java // Java code to illustrate the keySet() method // to get Set view of Keys from a Hashtable. import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Set; public class Example1 { public static void main(String[] args) { // Creating an empty Hashtable Hashtable<String, String> hash_t = new Hashtable<String, String>(); // Add mappings into the table hash_t.put("1", "Geeks"); hash_t.put("2", "For"); hash_t.put("3", "Geeks"); // Getting a Set of keys using // keySet() method of Hashtable class Set hash_set = hash_t.keySet(); System.out.println( "Set created from Hashtable Keys contains :"); // Iterating through the Set of keys Iterator itr = hash_set.iterator(); while (itr.hasNext()) System.out.println(itr.next()); } } OutputSet created from Hashtable Keys contains : 3 2 1 Example 2: Java // Java code to illustrate the keySet() method // to get Set view of Keys from a Hashtable. import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Set; public class Example2 { public static void main(String[] args) { // Creating an empty Hashtable Hashtable<String, String> hash_t = new Hashtable<String, String>(); // Inserting elements into the table hash_t.put("Geeks", "1"); hash_t.put("For", "2"); hash_t.put("geeks", "3"); // Getting a Set of keys using // keySet() method of Hashtable class Set hash_set = hash_t.keySet(); System.out.println( "Set created from Hashtable Keys contains :"); // Iterating through the Set of keys Iterator itr = hash_set.iterator(); while (itr.hasNext()) System.out.println(itr.next()); } } OutputSet created from Hashtable Keys contains : For Geeks geeks Note: The same operation can be performed with any type of Mappings with variation and combination of different data types. Comment More infoAdvertise with us Next Article Hashtable keySet() Method in Java with Examples S sayaliparulekar Follow Improve Article Tags : Java Java-Collections Java-HashTable Practice Tags : JavaJava-Collections Similar Reads Map keySet() Method in Java with Examples This method is used to return 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. Example:Java// Java Program Implementing Map // keySet() Method import java.util.*; public class GfG { public static void main(Str 2 min read KeyStore getKey() method in Java with Examples The getKey() method of java.security.KeyStore class is used to get the key associated with the given alias, using the given password to recover it. Syntax: public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException Parameter: 3 min read LinkedHashSet hashCode() method in Java with Example The hashCode() method of LinkedHashSet in Java is used to get the hashCode value for this instance of the LinkedHashSet. It returns an integer value which is the hashCode value for this instance of the LinkedHashSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: 2 min read TreeMap keySet() Method in Java with Examples In Java, keySet() method of TreeMap class is present inside java.util package in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in ascending order. Since the set 3 min read HashTable putIfAbsent() method in Java with Examples The putIfAbsent(Key, value) method of Hashtable class which allows to map a value to a given key if given key is not associated with a value or mapped to null. A null value is returned if such key-value set is already present in the HashMap. Syntax: public V putIfAbsent(K key, V value) Parameters: T 2 min read Like