We can use entrySet() method get all key-value pairs from hashtable in java. It returns a Set object with all key-value pairs.
Syntax:
hashtable.entrySet();
Example:
package com.w3schools; import java.util.Hashtable; import java.util.Map.Entry; 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<Entry<String, String>> entries = hashtable.entrySet(); for(Entry<String,String> entry:entries){ System.out.println(entry.getKey()+" - "+entry.getValue()); } } } |
Output
{3=Vivek, 2=Mahesh, 1=Jai} 3 - Vivek 2 - Mahesh 1 - Jai |