// Importing the required library import java.util.Hashtable; // Creating a new hashtable HashtablemyHashtable = new Hashtable (); // Adding elements to the hashtable myHashtable.put("apple", 1); myHashtable.put("banana", 2); myHashtable.put("orange", 3);
// Retrieving elements from the hashtable int value1 = myHashtable.get("apple"); int value2 = myHashtable.get("banana"); // Printing the retrieved values System.out.println("Value for apple is: " + value1); System.out.println("Value for banana is: " + value2);
// Checking if a key exists in the hashtable boolean containsKey = myHashtable.containsKey("orange"); // Printing the result of the check if (containsKey) { System.out.println("The hashtable contains the key 'orange'"); } else { System.out.println("The hashtable does not contain the key 'orange'"); }In this example, we check if the key "orange" exists in the hashtable using the `containsKey()` method. We then print out a message indicating whether the key exists in the hashtable or not.