Search a value for a given key in a HashMap

前端 未结 4 1099
余生分开走
余生分开走 2021-02-05 06:51

How do you search for a key in a HashMap? In this program, when the user enters a key the code should arrange to search the hashmap for the corresponding value and

4条回答
  •  天涯浪人
    2021-02-05 07:35

    To decalare the hashMap use this:

     HashMap hm=new HashMap();
    

    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);
     }
    

提交回复
热议问题