Home Tutorial Java Core HashMap Java HashMap example.


Share on Google+Share on Google+
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.


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.:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 5, 2011

Recommend the tutorial

Advertisements Advertisements