how remove and get elements contained in a Hash table using collections in java
Previous | Home | Next |
In this example we shall see how can we remove and retrieve elements contained in a Hashtable.
For creating a Hashtable first of all we have to import the java.util package in which the java.util.Hashtable class is defined. In this example we have created a class named CollectionExample in which we have created a Hashtable and added some elements to it.
In this example we have used the following methods,
remove(Object key)This method removes the key and its corresponding value from the Hashtable.
get(Object key)
This method gets the value contained at the specified key in the method.
boolean isEmpty()
This method tests if the Hashtable is empty or not .ie. it maps no keys to values.
In this example we have created a Hashtable named ht and added some elements to it by using the add(Object key, Object value) method on it. Then we removed and get some elements of it by using the various methods.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String[] args) throws NullPointerException
{
// TODO Auto-generated method stub
try
{
Hashtable<Integer,String> ht=new Hashtable<Integer,String>(); // created a Hashtable named ht in which key is numeric and value is String types
ht.put(1, "r4r"); // adding the values to the hashtable using key-value pairs
ht.put(2, "development");
ht.put(3, "tutorials");
ht.put(4, "shashi");
ht.put(5, "software developer");
System.out.println("The size of the Hashtable ht is: "+ht.size()); // displaying the size of the Hashtable
System.out.println("The elements according to key are: ");
for(int i=1;i<=ht.size();i++)
{
System.out.println("key = "+i+","+"Value = "+ ht.get(i)); // displaying the key-value pair contained in the Hashtable
}
ht.remove(1);
ht.remove(2);
System.out.println("The size of the Hashtable ht is: "+ht.size()); // displaying the size of the Hashtable after removing some elements
System.out.println("Checks whether key 2 is in Hashtable or not: "+ht.containsKey(2)); // checks for the existence of certain key
System.out.println("The elements according to key are: ");
for(int i=1;i<=ht.size();i++)
{
System.out.println("key = "+i+","+"Value = "+ ht.get(i)); // displaying the key-value pair contained in the Hashtable after removing some elements
}
ht.put(2, "r4r"); // putting r4r at key 2 in the Hashtable
System.out.println("The set of values containing contents of Hashtable is: "+ht.entrySet()); // displaying the contents of Hashtable in a set
System.out.println("Checks whether the Hashtable is empty or not: "+ht.isEmpty()); // checks whether the Hashtable is empty or not
ht.clear(); // this empties the Hashtable, now table contains no values
System.out.println("Checks whether the Hashtable is empty or not: "+ht.isEmpty());
}
catch(NullPointerException e) // handling the NullPointer Exception which is thrown
{
e.printStackTrace();
}
}
}
The output of the above created example is as follows:
The size of the Hashtable ht is: 5
The elements according to key are:
key = 1,Value = r4r
key = 2,Value = development
key = 3,Value = tutorials
key = 4,Value = shashi
key = 5,Value = software developer
The size of the Hashtable ht is: 3
Checks whether key 2 is in Hashtable or not: false
The elements according to key are:
key = 1,Value = null
key = 2,Value = null
key = 3,Value = tutorials
The set of values containing contents of Hashtable is: [5=software developer, 4=shashi, 3=tutorials, 2=r4r]
Checks whether the Hashtable is empty or not: false
Checks whether the Hashtable is empty or not: true
Previous | Home | Next |