Tags

 

— Used to store data by mapping a key to a value.

—  Used to store data during run-time.

— Key and value can be of any data type, for e.g. you can have a key of String type and value of integer type.

— Hashtable class extends Dictionary class and implements Map.

— Used to create dictionaries or associative array.

— Used when you want a fast lookup, e.g internet routers.

</p><p>import java.util.Hashtable;</p><p>public class HashTableTest {</p><p>public static void main(String[] args) {</p><p>// Creating a Hashtable
 Hashtable car = new Hashtable();

 // Entering the values (key-value) pair in hastables
 car.put("BMW",34500);
 car.put("Mazda",456765);
 car.put("Hyundai", 67876);
 car.put("Toyota", 788976);
 // Above hashtable stores prices for cars (imaginative!!)

 //Retreiving values from a hashtable using the key
 System.out.println(car.get("BMW"));

 // Finding size of hastables
 System.out.println("Size of car hastable is : " + car.size());

 // Checking whether hashtable is empty
 System.out.println("Is hastable car empty?? " + car.isEmpty());
 // Checking existence of a key in a hashtable
 System.out.println(car.containsKey("Toyota")); // output = true
 System.out.println(car.containsKey("ABC")); // output = false

 // Checking Existence of a value in a hashtable
 System.out.println(car.containsValue(67876)); // output = true
 System.out.println(car.containsValue(6787)); // output = false

}
}
Advertisements