際際滷

際際滷Share a Scribd company logo
Hash table in java
HASHTABLE
J.SHIRISHA
Hash table in java
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:
void clear(): Resets and empties the Hashtable.
object 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 isnt found.
boolean containsKey(object key): Returns true if some
key equal to the key exists within the Hashtable. Returns
false if the key isnt found.
boolean containsValue(object value): Returns true if some
value equl to the exists within the hashtable. Returns false if the
value isnt found.
Enumeration elements(): Returns an enumeration of the values
contained in the Hashtable.object get(object key): Returns the
object that contains the value associated with the key. If the key is
not in the Hashtable, a null object is returned.
boolean 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 isnt 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.
void 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
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.
 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();
while(names.hasMoreElements())
Key=(String) names.nextElement();
System.out.println(key:+key+&value:+hashtable.get(key));
}
}
}
OUTPUT:
Key:key5 & value: orange
Key:key4 & value: grapes
Key:key3 & value: mango
Key:key2 & value:banana
Key:key1 & value: apple
Hash table in java

More Related Content

Hash table in java

  • 4. 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.
  • 5. 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
  • 6. 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.
  • 7. 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
  • 8. 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.
  • 9. METHODS AND DESCRIPTION: void clear(): Resets and empties the Hashtable. object 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 isnt found. boolean containsKey(object key): Returns true if some key equal to the key exists within the Hashtable. Returns false if the key isnt found.
  • 10. boolean containsValue(object value): Returns true if some value equl to the exists within the hashtable. Returns false if the value isnt found. Enumeration elements(): Returns an enumeration of the values contained in the Hashtable.object get(object key): Returns the object that contains the value associated with the key. If the key is not in the Hashtable, a null object is returned. boolean 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 isnt already in the Hashtable; returns the previous value associated with the key if the key is already in the hash table.
  • 11. Enumeration keys(): Returns an enumeration of the keys contains in the Hashtable. void 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
  • 12. 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
  • 13. 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.
  • 14. 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
  • 15. 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).
  • 16. COLLISION HANDLING IN HASHTABLE: SEPERATE CHAINING:
  • 17. 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();
  • 18. while(names.hasMoreElements()) Key=(String) names.nextElement(); System.out.println(key:+key+&value:+hashtable.get(key)); } } } OUTPUT: Key:key5 & value: orange Key:key4 & value: grapes Key:key3 & value: mango Key:key2 & value:banana Key:key1 & value: apple