Hashtable in Java

Hashtable & its feature ::1. Hashtable class is an important class of Collection framework.2. Hashtable extends generic Dictionary.3. Hashtable implements generic Map, cloneable and java.io.Serializable interface.4. Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. An instance of Hashtable has two parameters that affect its performance: initial capacity(default 16) and load factor(deafult .75). The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case of a “hash collision”, a single bucket stores multiple entries, which must be searched sequentially. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.The initial capacity and load factor parameters are merely hints to the implementation. The exact details as to when and whether the rehash method is invoked are implementation-dependent.Internal working of Hashtabl and HashMap are similiar.For Detail understanding Please see How does a HashMap work internally ?5.By default Hashtable stores the elements in natural sorting order and it gives unique elements.6. Hashtable is synchronized i.e. it is thread safe.7.Hashtable does not allow null key and null values.If you try to insert null key or value,you will get NullPointerException.8. Hashtable implementation provides average O(1) time cost for the basic operations put(), get(),remove() and containsKey().9.Hashtable uses Iterator interface to traverse the elements.While traversing the elements using iterator if the structure of the Hashtable is modified using remove method or re hashing of Hashtable is done,iterator will throw ConcurrentModificationException.So iterators for Hashtable are fail-fast.While iterating you can add new elements without getting ConcurrentModificationException.

Sample Program ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

packagecom.ai1tutorial.collection.map;

import java.util.Hashtable;

import java.util.Map;

publicclassHashTableDemo{

publicstaticvoidmain(String[]args){

Map<String,String>hashtable=newHashtable<String,String>();

hashtable.put("Ajay","Cricket");

hashtable.put("Bijay","Cricket");

hashtable.put("Jyoti","Badminton");

hashtable.put("Trupti","BabySitting");

hashtable.put("Deepti","Managing");

hashtable.put("Asha","Teaching");

hashtable.put("Goutam","Managing");

hashtable.put("Papu","Dance");

for(Map.Entry<String,String>entry:hashtable.entrySet()){

System.out.println(entry.getKey()+" "+entry.getValue());

}

}

}

Output ::

1

2

3

4

5

6

7

8

Deepti Managing

Ajay Cricket

Bijay Cricket

Asha Teaching

Jyoti Badminton

Papu Dance

Trupti BabySitting

Goutam Managing

Now let us see an example of ConcurrentModificationException.In the below program while iterating the Hashtable,i am modifying the structure of the Hashtable by removing an element.So I am getting ConcurrentModificationException.Example of ConcurrentModificationException ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

packagecom.ai1tutorial.collection.map;

import java.util.Hashtable;

import java.util.Map;

publicclassHashTableDemo{

publicstaticvoidmain(String[]args){

Map<String,String>hashtable=newHashtable<String,String>();

hashtable.put("Ajay","Cricket");

hashtable.put("Bijay","Cricket");

hashtable.put("Jyoti","Badminton");

hashtable.put("Trupti","BabySitting");

hashtable.put("Deepti","Managing");

hashtable.put("Asha","Teaching");

hashtable.put("Goutam","Managing");

hashtable.put("Papu","Dance");

for(Map.Entry<String,String>entry:hashtable.entrySet()){

System.out.println(entry.getKey()+" "+entry.getValue());

hashtable.remove("Papu");

}

}

}

Output ::

1

2

3

4

Deepti Managing

Exception inthread"main"java.util.ConcurrentModificationException

at java.util.Hashtable$Enumerator.next(Unknown Source)

at com.ai1tutorial.collection.map.HashTableDemo.main(HashTableDemo.java:17)

About the Author

Dear Friends,
I am A.K.M. .This online journal ai1tutorial.com is a humble effort from my side to all students and professionals who want to learn computer science related technologies.I am not a professional blogger and I write posts in this blog only in my leisure time.I may not be 100% perfect in all my posts,but your review comments can surely help me to make it 100% perfect.