HASHTABLE IS ONE OF THE CLASS OF COLLECTION FRAMEWORK
Size: 2.09 MB
Language: en
Added: Nov 06, 2018
Slides: 19 pages
Slide Content
Hashtable J.SHIRISHA
WHAT IS HASHTABLE?? It is similar to Hashmap. Hashtable is an implementation of a key-value pair data structure in java(where key and value are objects). Hashtable inherits dictionary class and implements Map interface. It is synchronized. It is found in java.util.Hashtable package.
DECLARATION OF HASHTABLE java.util.Hashtable class HASHTABLE CLASS DECLARATION: HASHTABLE CLASS PARAMETERS: Let's see the Parameters for java.util.Hashtable class . K : It is the type of keys maintained by this map. V : It is the type of mapped values. public class Hashtable < k,V > extends Dictionary<K,V> implements Map<K,V>, Cloneable , Serializable
IMPORTANT POINTS ABOUT HASHTABLE CLASS: A Hashtable is an array of list. Each list is known as a bucket. The position of bucket is identified by calling the hashcode () method. A Hashtable contains values based on the key. It contains unique elements. It may have not have any null key or value. It is synchronized. Hashtable is a legacy class (old version). Hashtable is traversed by Enumerator and Iterator. Hashtable inherits Dictionary class.
WORKING OF HASHTABLE: Working of Hashtable depends on various parameters. Initial capacity, load factor, size and Threshold value are the parameters which affect Hashtable performance. Initial capacity: This is the capacity of Hashtable to store number of key value pairs when it is instantiated. Default capacity is 11. Load Factor: A parameter responsible to determine when to increase size of Hashtable . Default load factor is 0.75. Size: Number of key value pairs in Hashtable . Threshold value: When number of key value pairs is more than threshold value, then Hashtable is resized
CONSTRUCTOR AND DESCRIPTION: Hashtable (): This is the default constructor to the Hashtable it instantiates the Hashtable class. Hashtable ( int size ): This constructor accepts an integer parameter an dcreates a Hashtable the has an initial size specified by the integer value size. Hashtable ( int size , float fillRatio ): This creates a Hashtable that has an initial size specified by size and a fill ratio specified by fillRatio . This ratio must be between 0.0 and 1.0, and it determines how full the Hashtable can be before it is resized upward. Hashtable ( Map < ? extends k , ? extends v > t): This constructs a Hashtable with the given mappings.
METHODS AND DESCRIPTION: v oid clear(): Resets and empties the Hashtable. o bject clone(): Returns a duplicate of the invoking object. boolean contains( object value ): returns true if some value equal to the value exists within the Hashtable. Returns false if the value isn’t found. b oolean containsKey ( object key ): Returns true if some key equal to the key exists within the Hashtable. Returns false if the key isn’t found.
b oolean containsValue ( object value ): Returns true if some value equl to the exists within the hashtable. Returns false if the value isn’t found. Enumeration elements(): Returns an enumeration of the values contained in the Hashtable. o bject get( object key ): Returns the object that contains the value associated w ith the key. If the key is not in the Hashtable, a null object is returned. b oolean isEmpty (): Returns true if the Hashtable is empty; returns false if it contains at least one key. object put( object key , object value ): Inserts a key and a value into the Hashtable. Returns null if the key isn’t already in the Hashtable; returns the previous value associated with the key if the key is already in the hash table.
Enumeration keys(): Returns an enumeration of the keys contains in the Hashtable. v oid rehash(): Increase the size of the Hashtable and rehashes all of its keys. object remove( object key ): Removes the key and its value. Returns the value associated with the key. If the key is not in the Hashtable, a null object is returned. int size(): Returns the number of entries in the Hashtable. String toString (): Returns the string equivalent of a Hashtable
difference between hashmap and hashtable: HASHMAP HASHTABLE It is not synchronized. It is synchronized. It allows one null key and many number of null values. It does not allow null keys or null values. HashMap is fast as it is not synchronizes. Hashtable is slow as it is synchronized. HashMap is a new class introduced in JDK 1.2. Hashtable is a legacy class(old version). We can make the HashMap as synchronized by calling this code Map m =Collections.synchronizedMap (hashMap); Hashtable is internally synchronized and can't be unsynchronized. HashMap is traversed by Iterator Hashtable is traversed by Enumerator and Iterator. Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast HashMap inherits AbstractMap class. Hashtable inherits Dictionary class
ADVANTAGES OF HASHTABLE: Hashtable may have not have any null key or value. Hashtable is an implementation of a key-value pair data structure in java . You can store and retrieve a ‘value’ using a ‘key’ and it is an identifier stored. It is obvious that the ‘key’ should be unique. They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches and sets.
DISADVANTAGES OF HASHTABLE: You will be limited by available memory. Hashtable becomes quite inefficient when there are many collisions. Hash collisions are practically unavoidable. When hashing a random subset of a large set of possible keys. Hashtable does not allow null values, like hash map
COLLISION IN HASHTABLE: When you pass a key/value to the Hashtable , it queries the key's hashcode . The Hashtable uses that code to determine the bucket in which to place the key/value. In Java , the Hashtable responds to a collision by placing multiple values into the same bucket (other implementations may handle collisions differently).
COLLISION HANDLING IN HASHTABLE: SEPERATE CHAINING:
EXAMPLE PROGRAM TO DEMONSTRATE HASHTABLE: import java.util. Hashtable ; import java.util. Enumeration ; public class HashtableExample { public static void main( String [] args ) { Enumeration names; String key; // Creating a Hashtable Hashtable < String , String > hashtable = new Hashtable < String , String >(); //Adding key and value pairs to Hashtable hashtable.put ( “key1”, “ apple ” ); hashtable.put ( “key2”, “ banana ” ); hashtable.put ( “key3”, “ mango ” ); hashtable.put ( “key4”, “ grapes ” ); hashtable.put ( “key5”, “ orange ” ); names= hashtable.keys ();