Hashmap And Hashtable

Hashmap And Hashtable

Guys, One of the important concepts that you should know in java is the Hashmap and the Hashtable. This is one of the important questions raised in majority of the interviews. You should know to explain them and you should know to spot the difference between Hashmap and Hashtable. You can get an idea on this concept below.

Hashmap and Hashtable both comes under java collections. Both provide key values in order to access the data. Hashtable comes under original collection class in java whereas hashmap comes under collection framework part. We can access the data in the hashtable in synchronized manner whereas it is not possible to access data in synchronized manner in hashmap. Iterator in hashmap is fail safe. Hashmap allows NULL values and one NULL key whereas hashtable does not allow NULL values. HashMap is good for non threaded applications.

Hashmap

import java.util.HashMap;
public class hashmap
{
public static void main (String [] args)
{
HashMap<Integer, String> om = new HashMap<Integer, String> ();
om.put (new Integer (1), "insurance");
om.put (new Integer (2), "bank");
System.out.println ("Displaying Elements:" + om);
}
}
Hashtable

import java.util.Hashtable;
public class Hashtable
{
public static void main (String [] args)
{
Hashtable hasht = new Hashtable ();
hasht.put ("insurance", new Integer (1));
hasht.put ("bank", new Integer (2));
Object ob = hasht.get ("insurance");
System.out.println (ob);
}

No comments:

Post a Comment

My Profile