All About Class Hashtable in Java

0
99
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Hashtable in Java
Class Hashtable in Java

Hashtable in Java

 

All About Class Hashtable in Java

Hashtable is a subclass of Dictionary class which maps unique keys to values. Values can be duplicated. Hashtable also implements the interfaces like Map, CloneableAny  and Serializable.The Hashtable is used to store values in the form of Map key with value.The key needs to be non null object and corresponding value also needs to be non null.To successfully store and retrieve objects from a  Hashtable,the objects used as keys must implement the hashCode() and equals() methods.An instance of Hashtable has two parameters that affects its efficiency. They are:

  • Capacity
  • Load factor(range between 0.0 to 1.0)

The importance of Loadfactor:

When the entries in the Hashtable exceeds the product of load factor and the current capacity, the capacity is increased by calling rehash() method.More the larger the load factor, more the Hashtable will use memory efficiently,at the expense of larger expected time per lookup.If many entries are to be performed into Hashtable,we need to create a large capacity Hashtable that will allow entries to inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.As it extends the Dictionary class,hence it will get all the methods defined in the Dictionary class.

an example of Hashtable:


//create a Hashtable
Hashtable numbers=new Hashtable();
//insert objects into Hashtable
numbers.put("one",new Integer(1));
numbers.put("two",new Integer(2));
numbers.put("three",new Integer(3));
retrive a number from Hashtable
Integer n=(Interger)numbers.get("two");
if(n!=null)
System.out.println("two"+n);
 

The class Hashtable is defined as:


public class java.util.Hashtable extends java.util.Dictionary implements 
java.lang.cloneable{
//constructors:
public Hashtable();//constructs a new empty Hashtable with default initial capacity-11
//and loadfactor as 0.75
public Hashtable(int initialCapacity);//constructs a new empty Hashtable with 
//the initial capacity specified in the argument.The load factor is default as 0.75.
public Hashtable(int initialCapacity,float loadFactor);//constructs a new empty Hashtable with 
//the initial capacity and loadFactor specified in the arguments.
public Hashtable(Map m);//creates a new Hashtable from the Map specified.
//Methods:
public void clear();
protected void rehash();
public Object clone();
public boolean contains(Object value);
public boolean isEmptry();
public boolean containsKey(Object key);
public Object get(Object key);
public Object put(Object key,Object value);
public Object remove(object key);
public int size();
public Enumeration elements();
public Enumeration keys();
public String toString();
}

public Hashtable(int initialCapacity) throws IllegalArgumentException if the initial capacity is less than or equal to zero

An example:


import java.util.*;
public class HashtableTest{
     public static void main(String []args){
        Hashtable ht=new Hashtable();
        ht.put("Item-1","Apple");
        ht.put("Item-2","Orange");
        ht.put("Item-3","Banana");
        Enumeration e=ht.keys();
        while(e.hasMoreElements())
        {
            String st=(String)e.nextElement();
            System.out.println(ht.get(st));
        }
     }
}
 

The output of the code:
$javac HashtableTest.java
Note: HashtableTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$java -Xmx128M -Xms16M HashtableTest
Banana
Orange
Apple

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here