how to pass some other map collection to the HashMap collection using collections in java
Previous | Home | Next |
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 |