KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Hashtable

java.util
Class Hashtable<K,V>

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>
Direct Known Subclasses:
Properties, UIDefaults
See Also:
Top Examples, Source Code, Object.equals(java.lang.Object), Object.hashCode(), rehash(), Collection, HashMap, TreeMap

public void clear()
See Also:
V, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[634]Create object pool
By Anonymous on 2004/02/03 10:41:35  Rate
//Create object pool 
 import java.util.*; 
 public class Pool 
  {  
     private static Pool instance = null; 
  
  
     private int capacity; 
     private Hashtable locked, unlocked; 
      
     private Pool (  )  
      {  
         capacity = 10; 
         locked = new Hashtable (  ) ; 
         unlocked = new Hashtable (  ) ; 
      }  
      
     public synchronized Pool getInstance  (  )   {  
         if  (  instance == null  )   {  
             instance = new Pool (  ) ; 
          }  
         return instance; 
      }  
      
     public synchronized Object checkOut (  )  
      {  
         Object o; 
         if (  unlocked.size (  )   >  0  )  
          {  
             Enumeration e = unlocked.keys (  ) ; 
             return unlocked.get (  e.nextElement (  )   ) ; 
          }  
          
         if  (  capacity  <  locked.size (  )  + unlocked.size (  )   )  
          {  
             o = new Object (  ) ; 
             locked.put (  o, new Long (  
             System.currentTimeMillis (  )   )   ) ; 
             return (  o  ) ; 
          }  else  {  
             //Handle error condition 
          }  
      
      }  
      
     public synchronized void checkIn (  Object o  )  
      {  
         locked.remove (  o  ) ; 
         unlocked.put (  o, new Long (  
         System.currentTimeMillis (  )   )   ) ; 
      }  
  }  
  
  
  
 //clear


[1144]Understand the operation of hashtable completely
By mk_rauf { at } yahoo { dot } com on 2005/09/15 01:06:23  Rate
//IF USE READ THIS CODE U WILL UNDERSTAND THE OPERATION OF HASHTABLE COMPLETELY 
  
  
 import java.util.*; 
  
  
 public class UseHashTable  {  
  
  
   public static void main ( String [  ]  args )   {  
     //How to use HashTable 
     //Begin 
     String aa= "INDIA"; 
     String bb="three"; 
     Hashtable hashtable = new Hashtable (  ) ; 
      
     //To add Key and Value for Hashtable 
     hashtable.put ( "one",new Integer ( 1 )  ) ; 
     //hashtable.put ( "two",null ) ;     
     //HashTable will Not Take NULL values, where as we can add NULL values to HashMap 
     //HashTable is synchronized where as HashMap is Unsynchronized 
     hashtable.put ( bb,"THREE" ) ; 
     hashtable.put ( "four",aa ) ; 
      
     //To see the size and to check whether Hashtable is empty or not 
     System.out.println ( "The size of hashtable = "+hashtable.size (  )  ) ; 
     System.out.println ( "If hashtable empty = "+hashtable.isEmpty (  )  ) ; 
      
     //The elements of hashtable are  ( by using iterator )   
     System.out.println ( "The elements of hashtable are  ( by using iterator )  " ) ; 
     Set set= hashtable.keySet (  ) ; 
     Iterator iter = set.iterator (  ) ; 
     int i=1; 
     while ( iter.hasNext (  )  )  {  
       System.out.println ( " "+i+" )  "+hashtable.get ( iter.next (  )  )  ) ; 
       i++; 
      }  
      
     //The elements of hashtable are  ( by using Enumeration )  
     System.out.println ( "The elements of hashtable are  ( by using Enumeration )  " ) ; 
     Enumeration enum = hashtable.keys (  ) ; 
     i=1; 
     while ( enum.hasMoreElements (  )  )  
      {  
       String values =  ( String )  enum.nextElement (  ) ; 
       System.out.println ( " "+i+" )  key ="+values+", value ="+hashtable.get ( values )  ) ; 
       i++; 
      }  
      
 //     Checks whether this key/value is present in the hashtable or not 
     System.out.println ( "Contains this \"one\" Key ="+hashtable.containsKey ( "one" )  ) ;  
     System.out.println ( "Contains this \"five\" Key ="+hashtable.containsKey ( "five" )  ) ;  
     System.out.println ( "Contains this \"aa\" Value ="+hashtable.containsValue ( aa )  ) ;  
     System.out.println ( "Contains this \"new Integer ( 6 ) \" Value ="+hashtable.containsValue ( new Integer ( 6 )  )  ) ; 
  
  
     //The value for the key specified 
     System.out.println ( "The value for the \"three\" key ="+hashtable.get ( "three" )  ) ; 
      
     //Remove the value for the sepcified key 
     hashtable.remove ( bb ) ; 
     System.out.println ( "The size of HashMap has reduced by one" ) ; 
     System.out.println ( "The size of HashMap = "+hashtable.size (  )  ) ; 
     System.out.println ( "The values of HashMap are ="+hashtable.values (  )  ) ; 
      
     //The HashCode for this HashTable 
     System.out.println ( "The HashCode for this HashTable ="+hashtable.hashCode (  )  ) ; 
      
     //Remove all the elements from the hashtable 
     hashtable.clear (  ) ; 
     System.out.println ( "If hashmap empty = "+hashtable.isEmpty (  )  ) ; 
    
     //End of HashTable 
    }  
  }  
 


public Object clone()
See Also:
Cloneable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[635]Simple cache
By Anonymous on 2004/04/21 06:02:31  Rate
//simple cache 
 import java.util.*; 
 public class Cache 
  {  
     private static Cache instance = null; 
     private Hashtable cache; 
     private Cache (  )  
      {  
         cache = new Hashtable (  ) ; 
      }  
      
     public synchronized Cache getInstance  (  )   {  
         if  (  instance == null  )   {  
             instance = new Cache (  ) ; 
          }  
         return instance; 
      }  
      
     public synchronized void addObject (  Object key, Object value  )  
      {  
         cache.put (  key, value  ) ; 
      }  
      
     public Object getObject (  Object key  )  
      {  
         return cache.get (  key  ) ; 
      }  
      
     public void expire (  )  
      {  
         cache.clear (  ) ; 
      }  
      
     public synchronized void expire (  Object key  )  
      {  
         cache.remove (  key  ) ; 
      }  
  }  
  
  
 //clone


public boolean contains(Object value)
See Also:
Map, containsValue(Object), containsKey(Object), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean containsKey(Object key)
See Also:
contains(Object), NullPointerException, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean containsValue(Object value)
See Also:
Map, NullPointerException, V
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration<V> elements()
See Also:
Map, values(), keys(), Dictionary
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Set<Map.Entry<K,V>> entrySet()
See Also:
Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean equals(Object o)
See Also:
V, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public V get(Object key)
See Also:
put(Object, Object), NullPointerException, Dictionary, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int hashCode()
See Also:
Object, V, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Hashtable()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1546]New ways to synchronize hashmap
By Anonymous on 2005/09/25 18:26:44  Rate
//You should use ConcurrentHashMap in 1.5, no more Hashtable 
  
  
 Map myMap = new ConcurrentHashMap (  ) ;


[1571]_
By Anonymous on 2005/10/14 16:51:09  Rate
you can also use 
  
  
 Collections.synchronizedMap


public Hashtable(int initialCapacity)
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1675]Difference between HashMap and Hashtable
By Anonymous on 2005/11/04 20:23:08  Rate
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.  ( HashMap allows null values as key and value whereas Hashtable doesnt allow ) . HashMap does not guarantee that the order of the map will remain constant over time. HashMap is non-synchronized and Hashtable is synchronized.

public Hashtable(int initialCapacity,
                 float loadFactor)
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Hashtable(Map<? extends K,? extends V> t)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isEmpty()
See Also:
V, Dictionary, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration<K> keys()
See Also:
Map, keySet(), elements(), Dictionary
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Set<K> keySet()
See Also:
Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object put(Object key,
                  Object value)
See Also:
get(Object), Object.equals(Object), NullPointerException, Dictionary, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[550]Sort Hashtable
By Ashok Das on 2003/12/01 07:38:06  Rate
import java.util.Hashtable; 
 import java.util.Vector; 
 import java.util.Collections; 
 import java.util.Enumeration; 
  
  
 public class SortHashtable  {  
  
  
   public static void main ( String [  ]  args )   {  
     // Create and populate hashtable 
     Hashtable ht = new Hashtable (  ) ; 
     ht.put ( "ABC", "abc" ) ; 
     ht.put ( "XYZ", "xyz" ) ; 
     ht.put ( "MNO", "mno" ) ; 
      
     // Sort hashtable. 
     Vector v = new Vector ( ht.keySet (  )  ) ; 
     Collections.sort ( v ) ; 
      
     // Display  ( sorted )  hashtable. 
     for  ( Enumeration e = v.elements (  ) ; e.hasMoreElements (  ) ; )   {  
       String key =  ( String ) e.nextElement (  ) ; 
       String val =  ( String ) ht.get ( key ) ; 
       System.out.println ( "Key: " + key + " Val: " + val ) ; 
      }  
    }  
  }  
 


public V put(K key,
             V value)
See Also:
get(Object), Object.equals(Object), NullPointerException, Dictionary, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void putAll(Map<? extends K,? extends V> t)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void rehash()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public V remove(Object key)
See Also:
NullPointerException, Dictionary, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int size()
See Also:
V, Dictionary, Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Collection<V> values()
See Also:
Map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags