Advertisement



< Prev
Next >



Hashtable Class



Hashtable class implements Map interface. Hashtable stores the data in the form of key-value pairs, where key and value are objects. Hashcode of each key is computed internally and this value of hash code is used as an index at which the value is stored in the hash table. This is how the association between each key-value pair is formed.




Some important features of Hashtable







A simple constructor of Hashtable class


Hashtable()
This constructor creates an empty Hashtable.
Example -
Hashtable <Integer,Integer> ht = new Hashtable <Integer,Integer>();
This constructor creates a Stack to hold key-value pair of Integer objects.




Some methods in a Hashtable.


As Hashtable implements Map interface, so some methods are inherited from Map.

Methods Description
void clear() Removes all the key/value pairs from the Hashtable.
int size() Returns the total number of key-value pairs in a Hashtable.
boolean isEmpty() Returns true if Hashtable has no key-value pair in it.
V get(Object key) Returns the value associated with a specified key.
put(K key, V value) Puts the specified key and its associated value in the Hashtable.
Set<Map.Entry<K,V>> entrySet() Returns the Set containing Map.Entry objects.





Hashtable example


In this example we will create a Hashtable to store objects in the form of key-value pair, where key is a String object and value is an Integer object. For a given a key, we can find its value. And in this example, we will also obtain a value on the basis of a particular key.

import java.util.*;

class HashtableDemo
{
public static void main(String... ar)
{
Hashtable <String,Integer> hm = new Hashtable <String,Integer>();

hm.put("Max", 1000);
hm.put("John", 4000);
hm.put("Tom", 2000);
hm.put("Ana", 6000);
hm.put("Rick", 5000);

System.out.println(hm);

System.out.println("Value at the key, Tom is "+ hm.get("Tom"));
System.out.println("Value at the key, Ana is "+ hm.get("Ana"));

}

}

Output is :

{Max=1000, Tom=2000, John=4000, Rick=5000, Ana=6000}
Value at the key Tom is 2000
Value at the key Ana is 6000


Program Analysis





Advertisement




Iterating key-value pairs in a HashMap using entrySet() method


In this example we will create another Hashtable to store objects in the form of key-value pair, where key is a String object and value is an Integer object. We will use entrySet() method to get a Set view of this Hashtable. We will iterate over each map entry(key-value pair) using for-each loop. Let's see the code -:

import java.util.*;

class HashtableDemo
{
public static void main(String... ar)
{
Hashtable <String,Integer> hm = new Hashtable <String,Integer>();

hm.put("Max", 1000);
hm.put("John", 4000);
hm.put("Tom", 2000);
hm.put("Ana", 6000);
hm.put("Rick", 5000);

System.out.println("Iterating Hashtable using Map.Entry in a for-each loop");
Set<Map.Entry<String,Integer>> set = hm.entrySet();


for(Map.Entry<String,Integer> mapE : set)
{
	System.out.print(mapE.getKey() + " : ");
	System.out.println(mapE.getValue());
}

}
}


Output is :

Iterating Hashtable using Map.Entry in a for-each loop
Max : 1000
Tom : 2000
John : 4000
Rick : 5000
Ana : 6000


Program Analysis





Please share this article -




< Prev
Next >
< Stack Class
Properties Class >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement