Java HashMap example.


 

Java HashMap example.

Java HashMap example.

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}

Ads