Get all keys from hashtable in java

We can use keySet() method get all keys from hashtable in java. It returns a Set object with all keys.

Syntax:

hashtable.keySet();

Example:

package com.w3spoint;
 
import java.util.Hashtable;
import java.util.Set;
 
public class Test {
  public static void main(String args[]){
	//Create Hashtable object.
	Hashtable<String, String> hashtable = new Hashtable<String, String>();
	//Add objects to the Hashtable.
	hashtable.put("1", "Jai");
	hashtable.put("2", "Mahesh");
	hashtable.put("3","Vivek");
        System.out.println(hashtable);
        Set<String> keys = hashtable.keySet();
        for(String key: keys){
            System.out.println(key);
        }
  }
}

Output

{3=Vivek, 2=Mahesh, 1=Jai}
3
2
1
Please follow and like us:
Content Protection by DMCA.com