Thursday, April 6, 2017

Java Hashtable class with example

Hashtable is almost same as HashMap which can store elements in the form of key-value pairs.But the main difference is Hashtable does not let you have null value for key and the methods of Hashtable are synchronized,so they make a longer time to execute compared to HashMap's methods. In the previous post i had discussed about Java HashMap with examples.  Like HashMap, Hashtable stores key/value pairs in a hash table. It doesn't allow neither key nor values can be null.

We can write Hashtable class as:

class Hashtable<K,V>

Here k represents the type of key element and V represents the type of value element.You must remember, a hash table can only store objects that override hashCode() and equals() methods that are defined by the Object.Fortunately, many 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() methods.

Create Hashtable:

Hashtable<String,Integer> hs=new Hashtable<String,Integer>();

In the above statement we did not mention any capacity for the Hashtable. The default initial capacity of this Hashtable will be taken as 11 and the load factor as 0.75.

In addition to the methods defined by the Map interface,which Hashtable now implements, Hashtable defines the legacy methods. These methods may throw NullPointerException if an attempt is made to use a null key or value.

Hashtable class methods:

  1. value put(key,value): this method stores key-value pair into the Hashtable
  2. value get(Object key): This method returns the corresponding value when the key is given.
  3. Set<k>KeySet(): This method , when applied on a Hashtable coverts it into a Set where only keys will be stored.
  4. value remove(Object key): This method removes the key and corresponding value from the Hashtable
  5. boolen isEmpty(): This method returns true if there are no key-value pairs in the Hahstable
  6. void clear(): This method removes all the key-value pairs from the Hashtable
  7. int size(): this method returns the number of key-value pairs in the Hashtable
Let us understand Hashtable concept with exmaple:

import java.util.*;
import java.io.*;
class HashtableDemo
{
public static void main(String args[])throws IOException
{
//create Hashtable with names and phone numbers
Hashtable<String,Integer>ht=new Hashtable<String,Integer>();
ht.put("lucky",909099);
ht.put("pavan",888888);
ht.put("aadi",77777);
ht.put("eeswar",55555);
ht.put("mahi",44444);
ht.put("sagar",33333);
ht.put("Rudra",22222);
//display all names using enumerator
System.out.println("ALL student names");
Enumeration e=ht.keys();
while(e.hasMoreElements())
System.out.println(e.nextElement());
//accept name from the keyboard
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
String name=br.readLine();
name=name.trim();//remove unnecessary space
//get phone numbers 
Integer numbers=ht.get(name);
if(numbers!=null)
{
//covert numbers from Integer object to int value
Integer pno=numbers.intValue();
System.out.println(name+"number:"+pno);
}
else
System.out.println("name is not found");
}
}
Output:











Read Also:

Top 20 Collections interview questions
Java HashMap with example
Collection Framework tutorial
Java HashSet with examples
Java Comparator interface with Example



1 comment:

  1. Just wish to say your article is as astounding. The clearness in your
    submit is just excellent and i can suppose you’re knowledgeable in this subject.
    Well along with your permission allow me to take hold of your RSS feed to stay up
    to date with drawing close post. Thanks a million and
    please carry on the rewarding work.
    More here about AngularJS

    ReplyDelete

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...