Hashtable in java
Java Hashtable
class is available since jdk1.0 which is oldest class in java present in
java.util package. Java Hashtable
internally contains buckets where the key/value pairs are stored in buckets.
A Hashtable
class is an array of a list structure. Each list is known as a bucket. The
position of the bucket is identified by calling the hashcode() method.
Hashtable
is almost similar to HashMap except that Hashtable
is synchronized in nature.
Hashtable
class extends the Dictionary class and implements the Map interface and its
methods.
Hashtable - Here k means Key which is any data type
V means Value of any data types
Hashtable
class Declaration
public class Hashtable
extends
Dictionary
implements Map,
Cloneable, java.io.Serializable
{
…
}
Features
of Hashtable class
1] Hashtable stores unique key only. Duplicate key not
allowed if we try to enter the previous present key then recent one value get
override by previous one.
2] Hashtable
does not maintain insertion order.
3] TreeTable does not allow null key as well as null value
if we try to add null key then at runtime throws java.lang.NullPointerException
Exaception.
4] Hashtable
class is synchronized.
5] The
default initial capacity is 11 and
default loadFactor is 0.75.
In
the case of a "hash collision", a single bucket stores multiple
entries, which must be searched sequentially.
Constructor
and Description
Hashtable ht = new Hashtable()
Create a new, empty Hashtable object with a default initial
capacity (11) and load factor (0.75).
Hashtable ht = new Hashtable(int initialCapacity)
Create a new, empty Hashtable object with the specified initial
capacity and default load factor (0.75).
Hashtable ht = new Hashtable(int initialCapacity, float
loadFactor)
Create a new, empty Hashtable object with the specified initial
capacity and the specified load factor.
Hashtable ht = new Hashtable(Map t)
Create a new Hashtable object with the same mappings as the given
Map.
Java
Hashtable Example
package com.vr.techfloaters;
import java.util.Hashtable;
import java.util.Iterator;
public class HashTableDemo {
public static void main(String[] args) {
Hashtable ht = new Hashtable<>();
ht.put(60, "abc");
ht.put(70, "xyz");
ht.put(30, "ajd");
ht.put(10, "ajgh");
/*
Runtime
throws NullPointerException
ht.put(10,
null);
ht.put(null,
xyz);
*/
Iterator
itr = ht.keySet().iterator();
while (itr.hasNext()) {
Integer
key = (Integer) itr.next();
String
value = (String) ht.get(key);
System.out.println("Key: "+key+ " value:
"+value);
}
System.out.println("--------------------------------------");
// Java 8 forEach
method
ht.forEach((k,v) -> System.out.println(k+" "+v));
}
}
Output
Key: 10 value: ajgh
Key: 30 value: ajd
Key: 60 value: abc
Key: 70 value: xyz
--------------------------------------
70
xyz
60
abc
30
ajd
10
ajgh
From above output we can see duplicate key or value are not
allowed and also insertion order is not followed.
That’s all for a basic concept for Hashtable in
java, I hope that you enjoyed reading it and let us know in comment if anything
more info you want.
↧
0 comments:
Post a Comment