how tocreate a IdentityHashMap using the collection in java
Previous | Home | Next |
In this example we are going to create a IdentityHashMap.
For creating a IdentityHashMap we have to import the java.util package in which the java.util.IdentityHashMap class is contained. In this example we have created a class named CollectionExample in which we have created an object of the IdentityHashMap by using its default constructor. Then some elements are added to it.
In this example we have created a IdentityHashMap by using its default constructor as,IdentityHashMap<Integer, String> ihm=new IdentityHashMap<Integer, String>();The above statment constructs a new, empty identity hash map with a default size 21 and whose keys are of integer type and values are of string types.
We have added the elements to this IdentityHashMap by using the put(Object key, Object value) method on its object.
We have also created an object of HashMap and added all the elements of the IdentityHashMap to it by invoking the putAll(Map map) method on the object of the HashMap by passing the IdentityHashMap object to it as an argument, this is displayed by this statement,HashMap<Integer, String> hs=new HashMap<Integer, String>();
hs.putAll(ihm);
After adding some more elements to the HashMap, finally we have added all the elements of the HashMap to the IdentityHashMap by using the putAll(Map map) on its object.Finally we have displayed the contents of the IdentityHashMap.
In this example we have created a IdentityHashMap by using its default constructor as,
IdentityHashMap<Integer, String> ihm=new IdentityHashMap<Integer, String>();The above statment constructs a new, empty identity hash map with a default size 21 and whose keys are of integer type and values are of string types.
We have added the elements to this IdentityHashMap by using the put(Object key, Object value) method on its object.
We have also created an object of HashMap and added all the elements of the IdentityHashMap to it by invoking the putAll(Map map) method on the object of the HashMap by passing the IdentityHashMap object to it as an argument, this is displayed by this statement,
HashMap<Integer, String> hs=new HashMap<Integer, String>();
hs.putAll(ihm);
After adding some more elements to the HashMap, finally we have added all the elements of the HashMap to the IdentityHashMap by using the putAll(Map map) on its object.Finally we have displayed the contents of the IdentityHashMap.
package r4r.co.in;
import java.util.*;
public class CollectionExample
{
/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
IdentityHashMap<Integer, String> ihm=new IdentityHashMap<Integer, String>(); // creating a IdentityHashMap
ihm.put(1, "r4r"); // adding elements to the IdentityHashMap
ihm.put(2, "tutorials");
ihm.put(3, "development");
ihm.put(4, "softwares");
ihm.put(5, "shashi");
ihm.put(6, "s/w engineer");
Set<Map.Entry<Integer, String>> set=ihm.entrySet(); //creating set for holding the elements of the IdentityHashMap
Iterator<Map.Entry<Integer, String>> itr=set.iterator(); // Invoking iterator on the defined set
while(itr.hasNext())
{
Map.Entry<Integer, String> map=(Map.Entry<Integer, String>)itr.next(); // using the Map.Entry interface
int key=map.getKey(); // holding the keys in an int variable key
String value=map.getValue(); // holding the values in an String variable value
System.out.print("Key:= "+key); // displaying the keys
System.out.print(" Value:= "+value); // displaying the values
System.out.println();
}
/* Following code will demonstrate how can you pass the IdentityHashMap to some other collection
* as an argument and vice-versa
*/
HashMap<Integer, String> hs=new HashMap<Integer, String>(); // creating a new HashMap hs
hs.putAll(ihm); // adding the contents of IdentityHashMap to the HashMap
System.out.println("After adding IdentityHashMap the contents of HashMap are: "+hs); // displaying the initial contents of HashMap
hs.put(7, "e-learning"); // adding elements to the HashMap
hs.put(8, "r4r.co.in");
System.out.println("After addin elements to HashMap the contents of HashMap are: "+hs); // displaying the contents of the HashMap again
ihm.putAll(hs); // adding all the contents of the Hashmap to the IdentityHashMap
ihm.put(1, "r4r rocks!!!!!"); // overriding the element positioned at key 1
ihm.put(9, "r4r");
set=ihm.entrySet(); // creating a set
itr=set.iterator(); // Invoking iterator on the set defined
while(itr.hasNext())
{
Map.Entry<Integer, String> map=(Map.Entry<Integer, String>)itr.next();
int key=map.getKey();
String value=map.getValue();
System.out.print("Key:= "+key);
System.out.print(" Value:= "+value);
System.out.println();
}
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
The output of the above given example is as follows:
Key:= 3 Value:= development
Key:= 5 Value:= shashi
Key:= 2 Value:= tutorials
Key:= 6 Value:= s/w engineer
Key:= 1 Value:= r4r
Key:= 4 Value:= softwares
After adding IdentityHashMap the contents of HashMap are: {1=r4r, 2=tutorials, 3=development, 4=softwares, 5=shashi, 6=s/w engineer}
After addin elements to HashMap the contents of HashMap are: {1=r4r, 2=tutorials, 3=development, 4=softwares, 5=shashi, 6=s/w engineer, 7=e-learning, 8=r4r.co.in}
Key:= 3 Value:= development
Key:= 5 Value:= shashi
Key:= 8 Value:= r4r.co.in
Key:= 2 Value:= tutorials
Key:= 7 Value:= e-learning
Key:= 6 Value:= s/w engineer
Key:= 9 Value:= r4r
Key:= 1 Value:= r4r rocks!!!!!
Key:= 4 Value:= softwares
Previous | Home | Next |