how to pass the Hashtable as a parameter to some other collection and vice-versa using collections in java
Previous | Home | Next |
In this example we will pass a Hashtable as an argument to some other collection and vice-versa.
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 the following example we have used the putAll(Map map) to add all the element of the Hashtable to some other collection and vice-versa. The general syntax of putAll() method is given as,
void putAll(Map map)This method copies all of the mappings from the argument Map to the current Hashtable These mappings will replace any mappings of thecurrent Hashtable had for any of the keys currently in the specified Map.
In this example we have created a Hashtable and added elements to it by using the put(Object key, Object value) on the object of the Hashtable ht. We have also created HashMap and on the object of HashMap i.e map which is done as,Map<Integer, String> map=new HashMap<Integer, String>();we have used the putAll() method. This is done by the following statement,map.putAll(ht);
We have added some more elements to the HashMap using the put() method on the map.Then again we have used the putAll(Map map) method on the object of the Hashtable and passes the object of the HashMap as an argument to the method. Finally we have displayed the contents of the Hashtable.
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
}
Map<Integer, String> map=new HashMap<Integer, String>(); // creating a HashMap map
map.putAll(ht); // putting all the values contained in Hashtable ht to HashMap map
map.put(1, "ram"); /// this will replace the value contained at key 1 to ram
map.put(6, "krishna"); // adding the values to HashMap
map.put(7, "shiva");
System.out.println("The size of the Map map is: "+map.size()); // displaying the size of the HashMap
System.out.println("Now the contents of the created map are: ");
for(int i=1;i<=map.size();i++)
{
System.out.println("key = "+i+","+"Value = "+ map.get(i)); // displaying the contents of the HashMap map
}
ht.putAll(map); // adding all the contents of HashMap map to Hashtable ht
System.out.println("Now the size of the Hashtable ht is: "+ht.size()); // displaying the size of Hashtable ht
System.out.println("Now the contents of the Hashtable after merging the map contents are: ");
for(int i=1;i<=ht.size();i++)
{
System.out.println("key = "+i+","+"Value = "+ map.get(i)); // displaying the contents of the Hashtable ht
}
}
catch(NullPointerException e) // handling the NullPointer Exception which is thrown
{
e.printStackTrace();
}
}
}
The output of the above created example is as following:
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 Map map is: 7
Now the contents of the created map are:
key = 1,Value = ram
key = 2,Value = development
key = 3,Value = tutorials
key = 4,Value = shashi
key = 5,Value = software developer
key = 6,Value = krishna
key = 7,Value = shiva
Now the size of the Hashtable ht is: 7
Now the contents of the Hashtable after merging the map contents are:
key = 1,Value = ram
key = 2,Value = development
key = 3,Value = tutorials
key = 4,Value = shashi
key = 5,Value = software developer
key = 6,Value = krishna
key = 7,Value = shiva
Previous | Home | Next |