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);} } |
|
Elements in HashMap : {1=Bharat, 2=Gyan, 3=Sarika, 4=Vrishti, 5=Rohit, 6=Parineeta} |
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.