• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Hashtable




Hashtable
It is also similar to HashMap but the only difference is HashMap object is not synchronized, where as Hashtable object is synchronized that means only one thread is allowed at a time to work on reference.
Hashtable also offers some list of methods which are supported by hashMap.
Hashtable<wrapperclass1,wrapperclass2> objref=new Hashtable<wrapperclass1,wrapperclass2>();    
Hashtable put Method:
Which can be used to add a new element along with key element.
import java.util.*;
class HashtablePut
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
}
}

class Main
{
public static void main(String args[])
{
HashtablePut d=new HashtablePut();
d.list();
}
}
Hashtable get Method :
It is used to get a element based on key.
import java.util.*;
class HashtableGet
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.get(2));
}
}

class Main
{
public static void main(String args[])
{
HashtableGet d=new HashtableGet();
d.list();
}
}
 
Hashtable isEmpty Method:
Which return true if the map is empty otherwise it returns false
import java.util.*;
class HashtableIsEmpty
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.isEmpty());
}
}

class Main
{
public static void main(String args[])
{
HashtableIsEmpty d=new HashtableIsEmpty();
d.list();
}
}
Hashtable remove Method:
It is used to remove an element based on key value.
import java.util.*;
class HashtableIsRemove
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println("The Removing Element is "+al.remove(4));
}
}

class HashtableDemo
{
public static void main(String args[])
{
HashtableIsRemove d=new HashtableIsRemove();
d.list();
}
}
Hashtable size Method:
Returns number of key value mappings in the map.
import java.util.*;
class HashtableSize
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.size());
}
}

class Main
{
public static void main(String args[])
{
HashtableSize d=new HashtableSize();
d.list();
}
}
 
Hashtable keySet Method
Which can be used to get all the key elements which are existing in the specified Table.
import java.util.*;
class HashtableKeySet
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.keySet());
}
}

class Main
{
public static void main(String args[])
{
HashtableKeySet d=new HashtableKeySet();
d.list();
}
}
 
Hashtable Values Method
Which returns all the set of values which are available in the map.
import java.util.*;
class HashtableValue
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
System.out.println(al.values());
}
}

class Main
{
public static void main(String args[])
{
HashtableValue d=new HashtableValue();
d.list();
}
}
Hashtable Clear Method :
Which is used to remove all the key value pair from the specified map.
import java.util.*;
class HashtableClear
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
al.clear();
System.out.println(al);
}
}

class Main
{
public static void main(String args[])
{
HashtableClear d=new HashtableClear();
d.list();
}
}
 
Hashtable ContainsKey Method:
Which returns true if the map contains given key element
import java.util.*;
class HashtableContainsKey
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);

System.out.println(al.containsKey(2));

}
}

class HashtableDemo
{
public static void main(String args[])
{
HashtableContainsKey d=new HashtableContainsKey();
d.list();
}
}
 
Hashtable ContainsValue Method
Returns true if the given value is mapped by any key element otherwise returns false.
import java.util.*;
class HashtableContainsValue
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);

System.out.println(al.containsValue("chennai"));
}
}

class Main
{
public static void main(String args[])
{
HashtableContainsValue d=new HashtableContainsValue();
d.list();
}
}
Hashtable PutAll Method
Which is used to copy all the key value pair into another Table.
import java.util.*;
class HashtablePutAll
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
Hashtable<Integer,String> al2=new Hashtable<Integer,String>();
al2.putAll(al);
System.out.println(al2);
}
}

class Main
{
public static void main(String args[])
{
HashtablePutAll d=new HashtablePutAll();
d.list();
}
}
Hashtable Clone Method
Returns a duplicate copy from current map memory.
import java.util.*;
class HashtableClone
{
void list()
{
Hashtable<Integer,String> al=new Hashtable<Integer,String>();
al.put(1,"chennai");
al.put(2,"Delhi");
al.put(3,"Ap");
al.put(4,"Tg");
System.out.println("The List Items Are "+al);
Hashtable<Integer,String> al2;
al2=(Hashtable)al.clone();
System.out.println(al2);

}
}

class HashtableDemo
{
public static void main(String args[])
{
HashtableClone d=new HashtableClone();
d.list();
}
}