Hashtable example in Java

By: Ivan Lim Viewed: 198380 times  Printer Friendly Format    


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"));
}
}



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. hello
View Tutorial          By: paran at 2008-11-14 00:57:33

2. Here is good example to implement Hash table in ja
View Tutorial          By: Javaly at 2011-08-20 00:22:03

3. Finally a tutorial that clearly explains why Hasht
View Tutorial          By: Lisa Ander at 2011-10-27 20:33:15

4. Good job and Thanks
View Tutorial          By: Ehtasham Azhar at 2012-02-25 16:49:51

5. Thanks for the tutorial. I didn't get a lot of tra
View Tutorial          By: jordan at 2012-05-16 13:03:45

6. Thanks for the tutorial.

One questi

View Tutorial          By: Stephen at 2012-06-09 06:36:23

7. This is great I clearly understand how to use Hash
View Tutorial          By: Keith at 2012-06-14 20:18:37

8. Thanks for the explanication
View Tutorial          By: Koklen at 2012-06-20 03:40:05

9. Thanks!! Its very helpful.
View Tutorial          By: Tawfiiq at 2012-07-06 05:29:50

10. Thanks for the tutorial. I want to create a catalo
View Tutorial          By: Lue at 2012-07-09 07:00:08

11. Thank you for the tutorial. I want to create a cat
View Tutorial          By: Lue at 2012-07-09 10:40:08

12. Thank you... :)
Simple and effective explan

View Tutorial          By: Mohammed Muzammil at 2012-08-08 05:07:32

13. Hello sir, I observe that in the first version of
View Tutorial          By: Ganny at 2012-08-13 17:04:58

14. This is probably a good java tutorial but it made
View Tutorial          By: lol at 2012-09-15 21:20:09

15. Why can't you just use an array for something like
View Tutorial          By: Andrea at 2012-10-18 00:17:48

16. Thank you a lot for sharing your knowledge! It was
View Tutorial          By: Marcelo at 2013-01-17 03:07:37

17. I want to store the Data in Vector or the LinkedLi
View Tutorial          By: Sobia at 2013-01-17 11:24:59

18. Excellent explanation. How can I take input from t
View Tutorial          By: Joe at 2013-03-31 11:02:28

19. a very rare but useful tutorial. Thanks a bunch!
View Tutorial          By: Dalitso at 2014-02-19 12:07:34

20. You do not give an example on fillratio i want to
View Tutorial          By: reguieg at 2015-08-23 20:53:50

21. Ronaldfem
View Tutorial          By: Ronaldfem at 2017-01-23 13:20:28

22. RobertMt
View Tutorial          By: RobertMt at 2017-01-28 03:50:21

23. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-02-20 22:33:47

24. Thomasmi
View Tutorial          By: Thomasmi at 2017-02-22 20:28:22

25. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-02-26 03:28:50

26. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-03 16:20:41

27. Thomasmi
View Tutorial          By: Thomasmi at 2017-03-05 03:14:58

28. Douglassr
View Tutorial          By: Douglassr at 2017-03-07 23:20:18

29. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-08 23:31:34

30. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-15 19:00:39

31. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-17 00:03:55

32. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-17 01:00:03

33. RichardCemo
View Tutorial          By: RichardCemo at 2017-03-17 01:55:55

34. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-20 12:38:20

35. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-21 20:19:57

36. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-22 01:01:05

37. Thomasmi
View Tutorial          By: Thomasmi at 2017-03-23 09:14:30

38. Thomasmi
View Tutorial          By: Thomasmi at 2017-03-24 09:25:17

39. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-26 00:13:17

40. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-27 16:00:23

41. Thomasmi
View Tutorial          By: Thomasmi at 2017-03-28 01:14:50

42. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-29 16:39:49

43. RichardCemo
View Tutorial          By: RichardCemo at 2017-04-01 05:55:05

44. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-04-03 12:45:10

45. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-04-08 14:56:02

46. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-04-10 04:33:12

47. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-04-18 11:20:03

48. MerlinMub
View Tutorial          By: MerlinMub at 2017-04-29 03:06:20

49. Thomasmi
View Tutorial          By: Thomasmi at 2017-05-02 10:33:16

50. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-05-02 16:03:16

51. RichardCemo
View Tutorial          By: RichardCemo at 2017-05-02 21:51:57

52. Scottfocot
View Tutorial          By: Scottfocot at 2017-05-04 14:07:07

53. RichardCemo
View Tutorial          By: RichardCemo at 2017-05-13 23:30:55

54. RichardCemo
View Tutorial          By: RichardCemo at 2017-05-15 14:56:26

55. RichardCemo
View Tutorial          By: RichardCemo at 2017-06-03 17:25:37

56. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-06-13 07:04:41

57. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-06-14 19:15:17

58. CharlieTib
View Tutorial          By: CharlieTib at 2017-06-17 19:21:33

59. Donaldwax
View Tutorial          By: Donaldwax at 2017-07-22 06:29:05

60. MerlinMub
View Tutorial          By: MerlinMub at 2017-08-03 12:24:21

61. MerlinMub
View Tutorial          By: MerlinMub at 2017-08-04 15:21:53

62. RichardCemo
View Tutorial          By: RichardCemo at 2017-08-04 22:19:47

63. MerlinMub
View Tutorial          By: MerlinMub at 2017-08-12 05:32:42

64. GrahamDramp
View Tutorial          By: GrahamDramp at 2017-09-22 12:26:08