Search a value for a given key in a HashMap
To decalare the hashMap use this:
HashMap<Integer,String> hm=new HashMap<Integer,String>();
To enter the elements in the HashMap:
hm.put(1,"January");
hm.put(2,"Febuary");
before searching element first check that the enter number is present in the hashMap for this use the method containsKey() which will return a Boolean value:
if(hm.containsKey(num))
{
hm.get(num);
}
Just call get
:
HashMap<String, String> map = new HashMap<String, String>();
map.put("x", "y");
String value = map.get("x"); // value = "y"