Search key & value from HashTable - Java Program

Q. Write program to search key and value from HashTable.

Answer:

Below example of HashTable shows how to get the all keys as Enumeration object. The put() method is used to add the elements in the HashTable.  By using Enumeration methods like hasMoreElements() and nextElement() we can read all values from Hashtable. The containsKey( ) is used for checking the availability of the elements.

SearchValueKeys.java

import java.util.Enumeration;
import java.util.Hashtable;
class SearchValueKeys
{
        public static void main(String[] args)
        {
                Hashtable<String, String> ht = new Hashtable<String, String>();
                ht.put("1","First");
                ht.put("2","Second");
                ht.put("3","Third");
                ht.put("4","Forth");
                ht.put("5","Fifth");
                System.out.println("Hashtable elements with key: ");
                System.out.println(ht);
                System.out.println("===============================================");
                if (ht.containsKey("1") && ht.containsValue("First"))
                {
                        System.out.println("key 1 and value first are available.");
                }
                else
                        System.out.println("Hashtable doesn't contain first key and value");
                if (ht.containsKey("second") && ht.containsValue("2"))
                {
                        System.out.println("key second and value 1 are available.");
                }
                        System.out.println("Hashtable doesn't contain key second and value 2");
                        System.out.println("===============================================");
        }
}


Output:

search value keys