General

Hashtable in Java

General May 9, 2019
Hash table in java

Hashtable in Java

  • Java Hashtable class implements a Map Interface and extends Dictionary class
  • It is dual value pair collection in Java which stores <key,Value> pair in collection

The important points about Java Hashtable class are:

  • A Hashtable is an array of list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method.
  • A Hashtable contains values based on the key.
  • It contains only unique elements.
  • Value and key should not be NULL.
  • It is synchronized. (Thread safe, can be used in multi-threading environment).

Syntax

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

Example

package Mypkg;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MAPex {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                System.out.println("***********************************");
                Hashtable<Integer,String> hm=new Hashtable<Integer,String>(); 
                hm.put(100,"Besant"); 
                hm.put(600,"Chennai"); 
                hm.put(200,"Technology"); 
                hm.put(103,"Bangalore"); 
                            for(Map.Entry m:hm.entrySet()){ 
                 System.out.println(m.getKey()+"-> "+m.getValue()); 
                } 
                System.out.println("***********************************");
}
}

Output

*********************************

600-> Chennai

103-> Bangalore

200-> Technology

100-> Besant

***********************************

Click Here-> Get Prepared for Java Interviews

Methods of Hashtable

Method nameDescription
void clear()Removes all the key-value pairs.
boolean containsKey(Object key)Returns true, if the Hashtable contains a value for the specified key.
boolean contains value(Object value)Returns true, if the Hashtable maps a value for the specified key.
Enumeration<V> elements()Iterates  the values in the Hashtable.
Object get(Object key)Returns the value to which the specified key is mapped, or null if the map contains no mapping for the key.
Set entrySet()Returns a Sets which contains the key-value pairs of the map.
int hashCode()Returns the hash code value for the Hashtable
boolean isEmpty()Tests if this Hashtable maps no key value pairs.
Enumeration<K> keys()Returns the keys mapped in the Hashtable with the type Enumeration.
Set<K> keySet()Returns the keys mapped in the Hashtable as the type Set.
Value put(K key, V value)Maps the specified key with the specified value in the Hashtable.
void rehash()Increases the size of the Hashtable and re-maps the key values according to the new size.
V replace(K key, V value)Replaces the value of the specified key with the value specified.
int size()Returns the number of key-value pairs in the map.
Value remove(Object key)Removes the key value pair for the specified key from the map if it is present.

Click Here-> Get Java Training with Real-time Projects

Besant Technologies – Chennai & Bangalore Branch Locations

Besant Technologies - Velachery Branch

Plot No. 119, No.8, 11th Main road, Vijaya nagar,

Velachery, Chennai - 600 042

Tamil Nadu, India

Landmark - Reliance Digital Opposite Street

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Tambaram Branch

No.2A, 1st Floor, Duraisami Reddy Street,

West Tambaram, Chennai - 600 045

Tamil Nadu, India

Landmark - Near By Passport Seva

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - OMR Branch

No. 5/318, 2nd Floor, Sri Sowdeswari Nagar,

OMR, Okkiyam Thoraipakkam, Chennai - 600 097

Tamil Nadu, India

Landmark - Behind Okkiyampet Bus Stop,

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Porur Branch

No. 180/84, 1st Floor, Karnataka Bank Building,

Trunk Road, Porur, Chennai - 600 116

Tamil Nadu, India

Landmark - Opposite to Gopalakrishna Theatre

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Anna Nagar Branch

Plot No:1371, 28th street kambar colony,

I Block, Anna Nagar, Chennai - 600 040

Tamil Nadu, India

Landmark - Behind Reliance Fresh

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - T.Nagar Branch

Old No:146/2- New No: 48,

Habibullah Road,T.Nagar, Chennai - 600 017

Tamil Nadu, India

Landmark - Opposite to SGS Sabha

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Thiruvanmiyur Branch

22/67, 1st Floor, North mada street, Kamaraj Nagar

Thiruvanmiyur, Chennai 600041

Tamil Nadu, India

Landmark - Above Thiruvanmiyur ICICI Bank

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Siruseri Branch

No. 4/76, Ambedkar Street, OMR Road, Egatoor, Navallur,

Siruseri, Chennai 600130

Tamil Nadu, India

Landmark - Near Navallur Toll Gate, Next to Yamaha Showroom

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Maraimalai Nagar Branch

No.37, Ground Floor, Thiruvalluvar Salai,

Maraimalai Nagar,Chennai 603209

Tamil Nadu, India

Landmark - Near to Maraimalai Nagar Arch

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - BTM Layout Branch

No 2, Ground floor, 29th Main Road,

Kuvempu Nagar,BTM Layout 2nd Coming from Silkboard,

AXA company signal, Stage, Bangalore - 560 076

Karnataka, India

Landmark - Next to OI Play School

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Marathahalli Branch

No. 43/2, 2nd Floor, VMR Arcade,

Varthur Main Road, Silver Springs Layout,

Munnekollal, Marathahalli, Bengaluru - 560037

Karnataka, India

Landmark - Near Kundalahalli Gate Signal

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Rajaji Nagar Branch

No. 309/43, JRS Ecstasy, First Floor,

59th Cross, 3rd Block, Bashyam Circle,

Rajaji Nagar, Bangalore - 560 010

Karnataka, India

Landmark - Near Bashyam Circle

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Jaya Nagar Branch

No. 2nd Floor,1575,11th Main Road,

4th T-Block, Pattabhirama Nagar,

Jaya Nagar, Bangalore - 560 041

Karnataka, India

Landmark - Opposite to Shanthi Nursing Home

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Kalyan Nagar Branch

No.513, 4th Cross Rd

2nd Block, HRBR Layout,

Kalyan Nagar, Bangalore - 560 043

Karnataka, India

Landmark - Opposite to kalayan nagar Axis Bank

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Electronic City Branch

No. 7, 3rd Floor, Ganga Enclave,

Neeladri Road, Karuna Nagar, Doddathoguru Village,

Electronics City Phase 1, Electronic City,

Bangalore - 560100, karnataka, India

Landmark - Adjacent to HDFC Bank and State Bank Of India

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Indira Nagar Branch

No.54, 1st Floor,

5th Main kodihalli,

Bengaluru, Karnataka 560008, India

Landmark - Behind Leela Palace Hotel,

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - HSR Layout Branch

Plot No. 2799 & 2800, 27th Main,

1st Sector, HSR Layout,

Bengaluru, Karnataka 560102, India

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Hebbal Branch

No.29, 2nd Floor, SN Complex,

14th Main Road, E-Block Extention, Sahakara Nagar,

Bengaluru, Karnataka -560092, India

  +91-8767 260 270

  info@besanttechnologies.com

Scroll Up