Share on Google+Share on Google+

Java HashMap example.

Advertisement
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}

Advertisements

Advertisement

Posted on: April 5, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.