Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how tocreate a IdentityHashMap using the collection in java

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