how to pass some other map collection to the HashMap collection using collections in java
Previous | Home | Next |
In this example we are passing some other map collection to the HashMap that we have created.
For creating a HashMap first of all we have to import the java.util package in which the java.util.HashMap class is contained which is used for creating the HashMap. In this example we have created a class named CollectionExample in which we have created HashMap and added some elements to it.
For adding the elements of some collection, which extends the Map interface, to HashMap we have used the putAll(Map map) method on the object of the HashMap. The syntax of the putAll() method is ,
void putAll(Map map)
This method copies all of the mappings from the specified map to the HashMap These mappings will replace any mappings that this map had for any of the keys currently of the specified map.
In this example we have created a HashMap object and added some elements to it by using the put(Object key, Object value) on the object of the HashMap.
We have also created a Hashtable and added some elements to it. Then we have used the putAll(Map map) method on the object of the HashMap and passed the object of the Hashtable as an argument to it. Finally we have displayed the the contents of the HashMap.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
HashMap<String, Integer> hm = new HashMap<String, Integer>(); // creating a HashMap having keys in string format and values in Integer
hm.put("one-> ", 1); // adding values to the HashMap
hm.put("two-> ", 2);
hm.put("three-> ", 3);
hm.put("four-> ", 4);
hm.put("five-> ", 5);
System.out.println("The key contained in the HashMap are as follows: ");
Iterator<String> itr=hm.keySet().iterator(); // Invoking iterator on the key set of the hashmap
Iterator<Integer> itr1=hm.values().iterator(); // Invoking iterator on the value set of the hashmap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // displaying the keys of the hashmap
System.out.print(itr1.next()); // displaying the values of the hashmap
System.out.println();
}
Hashtable<String, Integer> htbl=new Hashtable<String, Integer>(); // creating a Hashtable named htbl
htbl.put("five->", 5); // adding values to the hashtable
htbl.put("six-> ", 6);
htbl.put("seven-> ", 7);
htbl.put("eight-> ", 8);
htbl.put("nine-> ", 9);
System.out.println("The contents of the created Hashtable htbl are: ");
itr=htbl.keySet().iterator(); // Invoking iterator on the keyset of the hashtable
itr1=htbl.values().iterator(); // Invoking iterator on the values set of the hashtable
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // displaying the keys of the hashtable
System.out.print(itr1.next()); // displaying the values of the hashtable
System.out.println();
}
hm.putAll(htbl); // adding the Hashtable collection to the HashMap
System.out.println("Now the contents of the HashMap after adding all the contents of the Hashtable are: ");
itr=hm.keySet().iterator(); // Invoking the iterator on the keyset of the resulted HashMap
itr1=hm.values().iterator(); // invoking the iterator on the value set of the resulted HashMap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // diplaying the keys of the resulted HashMap
System.out.print(itr1.next()); // displaying the values of the resulted HashMap
System.out.println();
}
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
}
}
The output of the example is given below as :
The key contained in the HashMap are as follows:
two-> 2
one-> 1
five-> 5
three-> 3
four-> 4
The contents of the created Hashtable htbl are:
nine-> 9
six-> 6
eight-> 8
seven-> 7
five->5
Now the contents of the HashMap after adding all the contents of the Hashtable are:
eight-> 8
six-> 6
nine-> 9
two-> 2
one-> 1
seven-> 7
five-> 5
three-> 3
five->5
four-> 4
As you can see in a HashMap there can be duplicate key-value pairs. As in the above example the key value pair of five->5 appears twice in the resulted HashMap after adding the contents of the Hashtable.
Previous | Home | Next |