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

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