This section explains the implementation of the hash table. What is the hash table and how to create that? Hash Table holds the records according to the unique key value. It stores the non-contiguous key for several values. Hash Table is created using an algorithm (hashing function) to store the key and value regarding to the key in the hash bucket. If you want to store the value for the new key and if that key is already exists in the hash bucket then the situation known as collision occurs which is the problem in the hash table i.e. maintained by the hashing function. The drawback is that hash tables require a little bit more memory, and that you can not use the normal list procedures for working with them.
This program simply asks you for the number of entries which have to entered into the hash table and takes one-by-one key and it's value as input. It shows all the elements with the separate key.
Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>():
Above code creates the instance of the Hashtable class. This code is using the type checking of the elements which will be held by the hash table.
Above method puts the values in the hash table regarding to the unique key. This method takes two arguments in which, one is the key and another one is the value for the separate key.
Map<Integer, String> map = new TreeMap<Integer, String>(hashTable):
Above code creates an instance of the TreeMap for the hash table which name is passed through the constructor of the TreeMap class. This code creates the Map with the help of the instance of the TreeMap class. This map has been created in the program for showing several values and it's separate key present in the hash table. This code has used the type checking.
Here is the code of the program:
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles