next


Java Online Compiler
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 aargs[])
{
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 aargs[])
{
HHashTableGet 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 aargs[])
{
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 aargs[])
{
HHashTableIsRemove 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 aargs[])
{
HashTableSize d=new HashTableSize();
d.list();
}
}

                            
                            

HashTable keySet Method
Which can be used to gert 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 aargs[])
{
HashTableKeySet d=new HashTableKeySet();
d.list();
}
}

HashTable Values Method
Which returns all the setof 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 aargs[])
{
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 aargs[])
{
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 aargs[])
{
HashTableContainsKey d=new HashTableContainsKey();
d.list();
}
}

HashTable ContainsValue Method
Returns true if the given value is mapped by any key element otherwise returns false.

iimport 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 aargs[])
{
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 aargs[])
{
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 HashMapDemo
{
public static void main(String aargs[])
{
HashTableClone d=new HashTableClone();
d.list();
}
}