{"id":169,"date":"2019-01-25T04:40:31","date_gmt":"2019-01-25T04:40:31","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=169"},"modified":"2019-03-02T04:39:24","modified_gmt":"2019-03-02T04:39:24","slug":"28-java-hashtables","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/java\/28-java-hashtables\/","title":{"rendered":"Java &#8211; Hashtables"},"content":{"rendered":"<p>In this lesson, I would teach you all you need to be able to use Hashtables in Java.<\/p>\n<p>A hashtable in Java is an implementation of the dictionary data structure. Therefore you can use it for storage of key-values pairs. This is similar to hashmaps. But unlike hashmaps, hashtables are thread-safe (or synchronized).<\/p>\n<p>&nbsp;<\/p>\n<p>We cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">How Hashtables Work<\/a><\/li>\n<li><a href=\"#t2\">Hashtable Methods<\/a><\/li>\n<li><a href=\"#t3\">Hashtable Example<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. How Hashtables Work<\/strong><\/h4>\n<p>To store an item in a hashtable, you need to specify the value you want to store. Also you must specify a key along with this value. Then this\u00a0 key is then hashed. This means passing it through a hash function. The output of the hashing is a hash code which represents the index where the value is to be stored.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to Create a Hashtable<\/strong><\/p>\n<p>You can create a hashtable using the new keyword. An example is given below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">\tHashtable ht <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Hashtable<span style=\"color: #333333;\">();<\/span>\r\n\tHashtable ht2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Hashtable<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>The first line creates a new hashtable while the second line creates a new hashtable with a size of 10. You can also use any of the constructors below.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Hashtable constructors<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>Hashtable( )<\/b><\/p>\n<p>The default constructor of the hash table. It creates a new hashtable.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>Hashtable(int size)<\/b><\/p>\n<p>Takes the initial size of the table as parameter and then creates a hash table that with an initial size given by the value size.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>Hashtable(int size, float fillRatio)<\/b><\/p>\n<p>This creates a hash table with an initial size given by size parameter and also a fill ratio. The fill ratio must be between 0.0 and 1.0. It then determines how full the hash table can be before it is resized.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>Hashtable(Map &lt; ? extends K, ? extends V &gt; t)<\/b><\/p>\n<p>This constructor creates a Hashtable with the given mappings.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.1. Hashtable Constructors<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Hashtable Methods<\/strong><\/h4>\n<p>You can use the following methods the perform various operations on a hashtable. Similar to arraylist, you can add and delete items from a hashtable.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>Sr.No<\/th>\n<th>Method &amp; Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>clear( )<\/b><\/p>\n<p>This is used to clear (or empty) the hash table.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>clone( )<\/b><\/p>\n<p>Used to return a duplicate of the invoking hashtable. Create another copy<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>contains(Object value)<\/b><\/p>\n<p>You use this to check if the hashtable contains the specified value. It true if\u00a0 the object exists within the hash table. Otherwise, it returns false<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>containsKey(Object key)<\/b><\/p>\n<p>You use this to check if the hashtable contains the specified key. It true if\u00a0 the key exists within the hash table. Otherwise, it returns false<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>containsValue(Object value)<\/b><\/p>\n<p>You use this to check if the hashtable contains the specified value. It true if\u00a0 the value exists within the hashtable. Otherwise, it returns false<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>elements( )<\/b><\/p>\n<p>Used to return an enumeration of all the values contained in the hashtable.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><b>get(Object key)<\/b><\/p>\n<p>Used to return the object that contains the value associated with the key. If the key is not in the hash table, a null object is returned.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><b>isEmpty( )<\/b><\/p>\n<p>Used to check if the hashtable is empty. Return true if the hash table is empty. Otherwise returns false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">9<\/td>\n<td><b>keys( )<\/b><\/p>\n<p>List of the keys contained in the hash table.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">10<\/td>\n<td><b>put(Object key, Object value)<\/b><\/p>\n<p>You use this to add a key and a value pair into the hash table. It returns null if the key is not already existing in the hashtable; returns the previous value associated with the key if the key is already in the hashtable.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">11<\/td>\n<td><b>rehash( )<\/b><\/p>\n<p>It Increases the size of the hashtable and then rehashes all of its keys.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">12<\/td>\n<td><b>remove(Object key)<\/b><\/p>\n<p>Removes the specified key along with its value. Returns the value associated with the specified key. If the key is not found in the hashtable, a null object is returned.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">13<\/td>\n<td><b>size( )<\/b><\/p>\n<p>Returns an integer value of the number of entries in the hashtable.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">14<\/td>\n<td><b>String toString( )<\/b><\/p>\n<p>Creates the string equivalent of a hashtable.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.1: Hashtable Methods<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">3. Hashtable Example<\/strong><\/h4>\n<p>In the example below, we create a hashtable and insert some items. Then remove an item. We also demonstrate the use of some other methods of the hash table. I therefor suggest you try this code on your own.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Hashtable<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">HashTableDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\tHashtable ht <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Hashtable<span style=\"color: #333333;\">();<\/span>\r\n\tht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Monday\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Tuesday\"<\/span><span style=\"color: #333333;\">);<\/span>\t\r\n\tht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Wednesday\"<\/span><span style=\"color: #333333;\">);<\/span>\t\r\n\tht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Thursday\"<\/span><span style=\"color: #333333;\">);<\/span>\t\r\n\tht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Friday\"<\/span><span style=\"color: #333333;\">);<\/span>\t\r\n\tht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Saturday\"<\/span><span style=\"color: #333333;\">);<\/span>\t\r\n\tht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">put<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Sunday\"<\/span><span style=\"color: #333333;\">);<\/span>\t\r\n\t\r\n\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Is Empty? \"<\/span> <span style=\"color: #333333;\">+<\/span> ht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isEmpty<\/span><span style=\"color: #333333;\">());<\/span>\r\n\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Contains Tuesday? \"<\/span> <span style=\"color: #333333;\">+<\/span> ht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">contains<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Tuesday\"<\/span><span style=\"color: #333333;\">));<\/span>\r\n\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Value of 5: \"<\/span> <span style=\"color: #333333;\">+<\/span> ht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">));<\/span>\r\n\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Number of entries: \"<\/span> <span style=\"color: #333333;\">+<\/span> ht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">size<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\r\n\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>ht<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">toString<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: Demo of hashtables<\/p>\n<p>&nbsp;<\/p>\n<p>If you run the code, you would have the output below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Is Empty<span style=\"color: #333333;\">?<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span>\r\nContains Tuesday<span style=\"color: #333333;\">?<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span>\r\nValue of <span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">:<\/span> Friday\r\nNumber of <span style=\"color: #997700; font-weight: bold;\">entries:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">7<\/span>\r\n<span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">=<\/span>Sunday<span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">=<\/span>Saturday<span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">=<\/span>Friday<span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">=<\/span>Thursday<span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">=<\/span>Wednesday<span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">=<\/span>Tuesday<span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">=<\/span>Monday<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, I would teach you all you need to be able to use Hashtables in Java. A hashtable in Java is an implementation &hellip; <\/p>\n","protected":false},"author":395,"featured_media":170,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-169","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/169\/revisions\/171"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/170"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}