Hashtable example in Java
By: Ivan Lim
Hashtable was part of the original java.util and is a concrete implementation of a Dictionary. However, Java 2 reengineered Hashtable so that it also implements the Map interface. Thus, Hashtable is now integrated into the collections framework. It is similar to HashMap, but is synchronized.
Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, 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 hashCode() and equals() methods that are defined by Object. The hashCode() 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 hashCode() method. For example, the most common type of Hashtable uses a String object as the key. String implements both hashCode() and equals().
The Hashtable constructors are shown here:
Hashtable( )
Hashtable(int size)
Hashtable(int size, float fillRatio)
Hashtable(Map m)
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 fillRatio. This ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resized 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 m. The capacity of the hash table is set to twice the number of elements in m. The default load factor of 0.75 is used. The fourth constructor was added by Java 2.
The following example uses a Hashtable to store the names of bank depositors and
their current balances:
// Demonstrate a Hashtable
import java.util.*;
class HTDemo {
public static void main(String args[]) {
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("John Doe", new Double(3434.34));
balance.put("Tom Smith", new Double(123.22));
balance.put("Jane Baker", new Double(1378.00));
balance.put("Todd Hall", new Double(99.22));
balance.put("Ralph Smith", new Double(-19.08));
// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into John Doe's account
bal = ((Double)balance.get("John Doe")).doubleValue();
balance.put("John Doe", new Double(bal+1000));
System.out.println("John Doe's new balance: " +
balance.get("John Doe"));
}
}
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, Hashtable does 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 entrySet( ) or keySet( ). 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:
// Use iterators with a Hashtable.
import java.util.*;
class HTDemo2 {
public static void main(String args[]) {
Hashtable balance = new Hashtable();
String str;
double bal;
balance.put("John Doe", new Double(3434.34));
balance.put("Tom Smith", new Double(123.22));
balance.put("Jane Baker", new Double(1378.00));
balance.put("Todd Hall", new Double(99.22));
balance.put("Ralph Smith", new Double(-19.08));
// show all balances in hashtable
Set set = balance.keySet(); // get set-view of keys
// get iterator
Iterator itr = set.iterator();
while(itr.hasNext()) {
str = (String) itr.next();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into John Doe's account
bal = ((Double)balance.get("John Doe")).doubleValue();
balance.put("John Doe", new Double(bal+1000));
System.out.println("John Doe's new balance: " +
balance.get("John Doe"));
}
}
Archived Comments
1. GrahamDramp
View Tutorial By: GrahamDramp at 2017-09-22 12:26:08
2. MerlinMub
View Tutorial By: MerlinMub at 2017-08-12 05:32:42
3. RichardCemo
View Tutorial By: RichardCemo at 2017-08-04 22:19:47
4. MerlinMub
View Tutorial By: MerlinMub at 2017-08-04 15:21:53
5. MerlinMub
View Tutorial By: MerlinMub at 2017-08-03 12:24:21
6. Donaldwax
View Tutorial By: Donaldwax at 2017-07-22 06:29:05
7. CharlieTib
View Tutorial By: CharlieTib at 2017-06-17 19:21:33
8. CurtisQuef
View Tutorial By: CurtisQuef at 2017-06-14 19:15:17
9. CurtisQuef
View Tutorial By: CurtisQuef at 2017-06-13 07:04:41
10. RichardCemo
View Tutorial By: RichardCemo at 2017-06-03 17:25:37
11. RichardCemo
View Tutorial By: RichardCemo at 2017-05-15 14:56:26
12. RichardCemo
View Tutorial By: RichardCemo at 2017-05-13 23:30:55
13. Scottfocot
View Tutorial By: Scottfocot at 2017-05-04 14:07:07
14. RichardCemo
View Tutorial By: RichardCemo at 2017-05-02 21:51:57
15. CurtisQuef
View Tutorial By: CurtisQuef at 2017-05-02 16:03:16
16. Thomasmi
View Tutorial By: Thomasmi at 2017-05-02 10:33:16
17. MerlinMub
View Tutorial By: MerlinMub at 2017-04-29 03:06:20
18. CurtisQuef
View Tutorial By: CurtisQuef at 2017-04-18 11:20:03
19. CurtisQuef
View Tutorial By: CurtisQuef at 2017-04-10 04:33:12
20. CurtisQuef
View Tutorial By: CurtisQuef at 2017-04-08 14:56:02
21. CurtisQuef
View Tutorial By: CurtisQuef at 2017-04-03 12:45:10
22. RichardCemo
View Tutorial By: RichardCemo at 2017-04-01 05:55:05
23. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-29 16:39:49
24. Thomasmi
View Tutorial By: Thomasmi at 2017-03-28 01:14:50
25. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-27 16:00:23
26. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-26 00:13:17
27. Thomasmi
View Tutorial By: Thomasmi at 2017-03-24 09:25:17
28. Thomasmi
View Tutorial By: Thomasmi at 2017-03-23 09:14:30
29. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-22 01:01:05
30. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-21 20:19:57
31. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-20 12:38:20
32. RichardCemo
View Tutorial By: RichardCemo at 2017-03-17 01:55:55
33. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-17 01:00:03
34. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-17 00:03:55
35. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-15 19:00:39
36. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-08 23:31:34
37. Douglassr
View Tutorial By: Douglassr at 2017-03-07 23:20:18
38. Thomasmi
View Tutorial By: Thomasmi at 2017-03-05 03:14:58
39. CurtisQuef
View Tutorial By: CurtisQuef at 2017-03-03 16:20:41
40. CurtisQuef
View Tutorial By: CurtisQuef at 2017-02-26 03:28:50
41. Thomasmi
View Tutorial By: Thomasmi at 2017-02-22 20:28:22
42. CurtisQuef
View Tutorial By: CurtisQuef at 2017-02-20 22:33:47
43. RobertMt
View Tutorial By: RobertMt at 2017-01-28 03:50:21
44. Ronaldfem
View Tutorial By: Ronaldfem at 2017-01-23 13:20:28
45. You do not give an example on fillratio i want to understand it
View Tutorial By: reguieg at 2015-08-23 20:53:50
46. a very rare but useful tutorial. Thanks a bunch!
but how can i use the map or HashMap
View Tutorial By: Dalitso at 2014-02-19 12:07:34
47. Excellent explanation. How can I take input from the commandline and add them to Hashtable in random
View Tutorial By: Joe at 2013-03-31 11:02:28
48. I want to store the Data in Vector or the LinkedList , and then that in HashTable ,
View Tutorial By: Sobia at 2013-01-17 11:24:59
49. Thank you a lot for sharing your knowledge! It was very helpful for me.
View Tutorial By: Marcelo at 2013-01-17 03:07:37
50. Why can't you just use an array for something like this? What is the advantage?
View Tutorial By: Andrea at 2012-10-18 00:17:48
51. This is probably a good java tutorial but it made me both angry and sad. Give me back my C# and Pyth
View Tutorial By: lol at 2012-09-15 21:20:09
52. Hello sir, I observe that in the first version of your program you use have inserted "John Doe&
View Tutorial By: Ganny at 2012-08-13 17:04:58
53. Thank you... :)
Simple and effective explanation.
View Tutorial By: Mohammed Muzammil at 2012-08-08 05:07:32
54. Thank you for the tutorial. I want to create a catalog with four fields mac?hinetype, bestcost, maxi
View Tutorial By: Lue at 2012-07-09 10:40:08
55. Thanks for the tutorial. I want to create a catalog with more than two columns i.e. machinename, bes
View Tutorial By: Lue at 2012-07-09 07:00:08
56. Thanks!! Its very helpful.
View Tutorial By: Tawfiiq at 2012-07-06 05:29:50
57. Thanks for the explanication
View Tutorial By: Koklen at 2012-06-20 03:40:05
58. This is great I clearly understand how to use Hashtable after this.
Stephen brings up
View Tutorial By: Keith at 2012-06-14 20:18:37
59. Thanks for the tutorial.
One question: Since Hashtable is a generic requiring two ty
View Tutorial By: Stephen at 2012-06-09 06:36:23
60. Thanks for the tutorial. I didn't get a lot of training or material on hashtable use and this really
View Tutorial By: jordan at 2012-05-16 13:03:45
61. Good job and Thanks
View Tutorial By: Ehtasham Azhar at 2012-02-25 16:49:51
62. Finally a tutorial that clearly explains why Hashtable is called "hash", better than JDK's
View Tutorial By: Lisa Ander at 2011-10-27 20:33:15
63. Here is good example to implement Hash table in java
http://onsite-interview.blogspot
View Tutorial By: Javaly at 2011-08-20 00:22:03
64. hello
View Tutorial By: paran at 2008-11-14 00:57:33
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program