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 create a HashMap using collections in java

how to create a HashMap using collections in java

Previous Home Next

 

In the following example we will see how to create a HashMap using collections.

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.
 In this example we have used the put(Object key, Object value) method on the object of the HashMap for adding the elements to the HashMap. The general form of the put() method is,


put(Object key, Object value)

This method puts the specified value with the specified key in the method to the HashMap.

In the following example we have created a HashMap which have the keys in String format and the values  are in the Integer format. Then we have used the put(Object key, Object value) method on the the HashMap object i.e hm(in this example). 
Finally we have displayed the HashMap thus created.

 
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
hm.put("one-> ", 1); // inserting 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 hashmap's keyset to retrieve the keys
while(itr.hasNext())
{
System.out.println(itr.next()); // displaying the keys in the hashMap
}
System.out.println("The values contained corresponding to the key are as follows: ");
Iterator<Integer> itr1=hm.values().iterator(); // Invoking iterator on the hashMap's elements
while(itr1.hasNext())
{
System.out.println(itr1.next()); //displaying the elements of the hashmap
}
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
}


}

The output of the above given example is as follows: 

The key contained in the HashMap are as follows:
two->
one->
five->
three->
four
The values contained corresponding to the key are as follows:
2
1
5
3
4

Previous Home Next