Hash table Java Help

Hash table 

Hash table was part of the original java.utit and is a concrete implementation of ~ Dictionary. However, Java 2 engineered Hash table so that it also Implements the Map interface. Til Us, Hash table is now integrated into the collections framework. It is similar to Hash Map, but is synchronized. Like Hash Map, Hash table skyrocket/value pairs in a hash table. When using a Hash table, you specify an object that is used as a key, and the value that you want linked to that key. The key.is then hashed, and the resulting hash code is used as the ‘ index at which the value is stored within the table.

A hash table can only store objects that override the hash Code( ) and equals ) methods that are defined by Object/The hash Code( ) method must compute and return the hash code for the object. Of course, equals( ) compares two objects. Fortunately, many of Java’s built-in classes already implement the hash Code( ) method. For example, the most common type of Hash table uses a String object as the key. String implements both hash Code( ) and equals( ). The Hash table constructors are shown here: Hash table( ) Hash table int size) Hash table(int size, floater Hash table(Map 1/1) The first version is the default constructor. The second version creates a hash table that has an initial size specified by size. The third version creates a hash table that has an , initial size specified by size and a fill ratio specified by fill Ratio. This ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resided upward. Specifically, when the number of elements is greater than the capacity of the , hash table multiplied by its fill ratio, the hash table is expanded. If you do  not specify. a fill ratio, then 0.75 is used. Finally, the fourth version creates a hash table that is .initialized with the elements in 1/1. The capacity of the hash table is set to twice the number of elements in 111. The default load factor of 0.75 is used. The fourth constructor was added by Java 2. In addition to the methods defined by the Map interface, which Hash table now implements; Hash table defines the legacy methods listed .

The following example reworks the bank account program, shown earlier, so that it uses a Hash table to store the names of bank depositors .and their current balances:

The output from this program is shown here

Ralph Smith: -19.08
Tom Smith: ‘123.22
John Doe: 3434.34
Todd Hall: 99.22
Jane Baker: 1378.0
John Doe’s new balance: 4434.34

One important point: like the map classes, Hash table docs not directly support iterators. Thus, the preceding program uses an enumeration to display the contents of balance. However, you can obtain set-views of the hash table, which permits the use of iterators. To do so, you simply use one of the collection-view methods defined by Map’, such as entry Set( ) or key Set( ). For example, you can obtain a set-view of the keys’ and iterate through them. Here is a reworked version of the program that shows this technique:

JavaHelpOnline.com

  • Feel free to send us an inquiry, we will reply back in hours.
 

Verification

Posted on September 17, 2014 in java.util More Utility Classes

Share the Story

About the Author

Back to Top