Sunday, 4 March 2018

Hashtable in Java

Hashtable is included in java.util package in java. It extends the Dictionary class and implements the Map interface.

Hashtable will work similar to Hashmap but it is synchronized and does not allow null as key in Hashtable.

Like Hashmap, Hashtable stores data as key value pair. To store the data Hashtable uses key and it's hashcode as index to store the value.

public class Hashtable<K,V>   extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable

Hashtable has below specified constructors:

1. public Hashtable(int initialCapacity, float loadFactor)
2. public Hashtable(int initialCapacity)
3. public Hashtable()
4. public Hashtable(Map<? extends K, ? extends V> t)

Below are some of methods available in Hashtable:

1. public synchronized int size() --- returns no of entries of Hashtable
2. public synchronized boolean isEmpty() -- to check whether Hashtable is empty or not. Returns true if empty otherwise return false.
3. public synchronized Enumeration<K> keys() -- returns enumeration of keys
4. public synchronized Enumeration<V> elements() --- -- returns enumeration of keys
5. public synchronized boolean contains(Object value) --- Returns true if given values is exist in Hashtable
6. public boolean containsValue(Object value) --- Returns true if given values is exist in Hashtable
7. public synchronized boolean containsKey(Object key) --- Returns true if given keyis exist in Hashtable
8. public synchronized Object get(Object key) ---Returns object that contains value associated Key
9. public synchronized Object put(K key, V value) ---Inserts key and value into Hashtable. It returns null if key is not there in Hashtable and returns previous value associated with Key if key is already exist in Hashtable.
10. public synchronized Object remove(Object key) --- Remove key and it's value. It retunrs null if key is not there or returns value associated with key if key available.
11. public synchronized void putAll(Map<? extends K, ? extends V> t)
12. public synchronized void clear() --- Reset and empty the Hashtable.
13. public synchronized Object clone() -- Returns duplicate of invoking object

Will explain these methods in below program:

package com.sample.java.testing;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;

public class HashtableTest {

    public static void main(String[] args) {

        Hashtable<String , String> hashtable = new Hashtable<>();
       
        //put
        hashtable.put("one", "Java");
        hashtable.put("two", "j2ee");
        hashtable.put("three", "spring");
        hashtable.put("four", "hibernate");
       
        //size
        System.out.println("size of hashtable is " + hashtable.size());
       
        //isEmpty
        System.out.println(hashtable.isEmpty());
       
        //keys
       
        for (Enumeration<String> keys = hashtable.keys(); keys.hasMoreElements();) {
            String key = keys.nextElement();
            System.out.println("keys of hastbale are.." + key);
           
        }
       
        //elements
        Enumeration<String> values = hashtable.elements();
       
        while (values.hasMoreElements()) {
            String string = (String) values.nextElement();
            System.out.println("elements of hastbale are.." + string);
        }
       
        //contains
        System.err.println(hashtable.contains("Java"));
       
        //containsValue
        System.out.println(hashtable.containsValue("j2ee"));
       
        //containsKey
        System.out.println(hashtable.containsKey("one"));
       
        //get
        System.out.println(hashtable.get("three"));
       
        //remove
        System.out.println(hashtable.remove("four"));
       
        //entryset       
         Set<Entry<String, String>> valueSet = hashtable.entrySet();
       
         for (Entry<String, String> entry : valueSet) {
             System.out.println("key is " + entry.getKey() + " value is " + entry.getValue());
           
        }
       
         //key set       
          Set<String> keySet = hashtable.keySet();
          for (String string : keySet) {
            System.out.println("keys is " + string + " value is " + hashtable.get(string));
        }
       
        //clone
        Hashtable<String, String> hashtable2 = (Hashtable<String, String>) hashtable.clone();
       
        for (Entry<String, String> entry :  hashtable2.entrySet()) {
            System.out.println("clone set key is " + entry.getKey() + " value is " + entry.getValue());
           
        }
       
        //clear
        hashtable2.clear();
        System.out.println(hashtable2.size());
    }

}

No comments:

Post a Comment