Hashtable in Java

Hashtable is  legacy class in Java.Vector is the thread safe version of  ArrayList.Similarly Hashtable is the thread safe version of HashMap.(But this thread  safety makes a performance downfall).The way by which data is inserted , iterated and deleted are shown the example code shown below.

import java.util.*;
public class HashtableSample {
HashtablehashTable = null;
public HashtableSample(){
}
public void addItems() {
System.out.println("Adding contents to Hashtable ");
hashTable = new Hashtable();
String[] listItems = {"dog", "cat", "cow", "elephant", "sheep"};
for (int i = 0; i < listItems.length; i++) { hashTable.put(i, listItems[i]); } } public void display() { System.out.println("Displaying contents of hashtable"); Set set = hashTable.entrySet(); System.out.println("Size = "+set.size()); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); System.out.print(entry.getKey() + ": "); System.out.println(entry.getValue()); } } public void removeItems() { int count = 0; while (count < 5) { hashTable.remove(count); count++; } System.out.println("Displaying contents after removal:"); display(); } public static void main(String[] args) { HashtableSample sample = new HashtableSample(); sample.addItems(); sample.display(); sample.removeItems(); } }

Now , let us see the output

Output

Adding contents to Hashtable

Displaying contents of hashtable

Size = 5

4: sheep

3: elephant

2: cow

1: cat

0: dog

Displaying contents after removal:

Displaying contents of hashtable

Size = 0

Difference between HashMap and Hashtable

1)Hashtable is the thread safe version of HashMap . Somethods are synchronized .Hence  performance downfall is there in case of Hashtable.

2)HashMap permits null values as well as one null key.But Hashtable never allows these.

 

Hashtable implementation depends on the hash code values of  objects  using as keys .  If we use objects of a class as keys in a Hashtable then   our class needs to override hashCode() and equals() method.Otherwise meaningfully same objects  may be stored multiple times. This concept is well explained here.

 

 

Tagged with 

3 thoughts on “Hashtable in Java

  1. I think this is among the so much significant info for me. And i am happy studying your article. But wanna statement on some common issues, The site style is ideal, the articles is in point of fact excellent : D. Good task, cheers

  2. […] c)Hashtable : It is a legacy class in Java.Hashtable is the thread safe version of HashMap.We know HashMap allows one null key and many null values. But hashtable never allows both.That is the difference between HashMap and Hashtable. […]

  3. tabletki poronne says:

    Thank you for any other informative web site. The place else may I am getting that kind of
    information written in such an ideal approach? I have a project that I am simply now
    running on, and I’ve been on the look out for such information.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">