HashMap Example

HashMap Example

Previous Home Next

 

This program shows how to get the key from the HashMap.

The HashMap provide the method to get the value from the map and that is get method .
 Get() method is the method provided by the HashMap class of java collections itself.
This method is used to get the particular value from the HashMap.

After passing values to the map in this example as (hmp.put) using. Make an object of Object class as obj refere the value particular key to the obj by passing it as the argument of get().

 

package Example;
import java .util.Enumeration;
import java .util.HashMap;
public class HashmapExample {


public static void main(String[] args) {
//first to create the HashMap
HashMap hmp =new HashMap();
//put the value in the HashMap
hmp.put("one", new Integer(1));
hmp.put("Two",new Integer (2));
hmp.put("Three",new Integer (3));

//to get the values of HashMap

Object obj= hmp.get("one");
//to check the existence of particular keyin map
//boolean blnExists =hmp.containsKey("3");
System.out.println("The value get from Map is:"+obj);
// TODO Auto-generated method stub

}

}

The value get from Map is:1

Previous Home Next