Delivered by FeedBurner

Java Hashtable Class

  • Hashtable Class implements Map, Cloneable and Serializable interfaces and also extends Dictionary class.
  • It maps keys to values in table format. Every key is linked to it's value.
  • In Hashtable, any object which is non null will be used as a key or it's value.
  • Initial capacity and load factor are two parameters of Hashtable that affect its performance.
  • Meaning of capacity is the number of buckets in Hashtable and Meaning of initial capacity is number of buckets in Hashtable at the time of its creation.
  • Load factor is parameter which measure how much full Hashtable is and when to increase it's capacity automatically.
  • In order to successfully retrieve and store object in Hashtable, the object which is used as key must implement the equals and the hashCode methods.
Important methods of Hashtable Class are given below.
  • void clear() : It will remove all key and it's values from Hashtable.
  • Object clone() : It will create and return shallow copy of this hashtable.
  • boolean contains(Object value) :It will check in Hashtable for any key maps to the specified value.
  • boolean containsKey(Object key) : It will check if any key is available in Hashtable which is same as specified object.
  • boolean containsValue(Object value) : It will return true if Hashtablehas any key which maps to specified value.
  • Enumeration<V> elements() : Returns an enumeration of the values in this Hashtable.
  • Set<Map.Entry<K,V>> entrySet() : It will return set view of Hashtable with mapping.
  • boolean equals(Object o) : It will check and compare given object with map for equality.
  • V get(Object key) : It will return value which is mapped with specified key in map. Return null if no mapping found.
  • int hashCode() : It will return has code value for this map.
  • boolean isEmpty() : It will check if Hashtable is empty and no mapping found.
  • Enumeration<K> keys() : It will returns an enumeration of the keys in this Hashtable.
  • Set<K> keySet() : It will return set view of keys contained in this map.
  • V put(K key, V value) : Map and insert specified key with value in map.
  • void putAll(Map<? extends K,? extends V> t) : It will copy all key-values mapping from specified map to this map.
  • protected void rehash() : It will increases the capacity of and internally reorganizes this Hashtable, in order to accommodate and access its entries more efficiently.
  • V remove(Object key) : It will remove specified key and its mapped value from
  • int size() : It will return size of Hashtable.
  • Collection<V> values() : It will return collection view of map.
I have created sample example for Hashtable to show usage of basic methods.

package JAVAExamples;

import java.util.Hashtable;
import java.util.Map;

public class HashTableExample {

 public static void main(String[] args) {
  Hashtable<Integer, String> ht = new Hashtable<Integer, String>();

  ht.put(1000, "London");
  ht.put(1001, "Mumbai");
  ht.put(1002, "Chicago");
  ht.put(1003, "Tokyo");
  ht.put(1004, "Paris");
  ht.put(1005, "Moscow");
  ht.put(1006, "Berlin");

  for (Map.Entry m : ht.entrySet()) {
   System.out.println(m.getKey() + " " + m.getValue());
  //Check if there is any key in map which maps to specified value.
  System.out.println("Map has any key for Chicago? : "+ht.contains("Chicago"));
  //Check if specified key is available in map.
  System.out.println("Map has key 1009? : "+ht.contains(1009));
  //Get size of map
  System.out.println("Map size is : "+ht.size());    

Output :
1004 Paris
1003 Tokyo
1002 Chicago
1001 Mumbai
Map has any key for Chicago? : true
Map has key 1009? : false
Map size is : 7

No comments:

Post a Comment