Home Tutorial Java Core HashMap Java HashMap example.

 
 

Java HashMap example.
Posted on: April 5, 2011 at 12:00 AM
Java HashMap example.

Java HashMap example.

The HashMap is a class in java. It stores values in name values pair. You can store null value of key and values.  

Here, you will see how to create an object of HashMap class. How to display vlaue of map.

Code

HashMapExample .java

package net.roseindia.java;

import java.util.HashMap;

public class HashMapExample {

public static void main(String[] arr) {

/* Create object of HashMap */

HashMap<Integer, String> obMap = new HashMap<Integer, String>();

/* Add value in HashMap */

obMap.put(new Integer(1), "Bharat");

obMap.put(new Integer(2), "Gyan");

obMap.put(new Integer(4), "Vrishti");

obMap.put(new Integer(3), "Sarika");

obMap.put(new Integer(5), "Rohit");

obMap.put(new Integer(6), "Parineeta");

/* Display value of HashMap */

System.out.println("Elements in HashMap : \n" + obMap);

}

}

Output :

Elements in HashMap :

{1=Bharat, 2=Gyan, 3=Sarika, 4=Vrishti, 5=Rohit, 6=Parineeta}

Related Tags for Java HashMap example.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.