Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the key when we know the value in HashMap [duplicate]

Possible Duplicate:
Java Hashmap: How to get key from value?

I know that a HashMap contains a particular integer variable as value. How can I get the key associated with this value?

like image 584
London guy Avatar asked Dec 13 '22 00:12

London guy


1 Answers

This code will do that:

  public List<Object> getKeysFromValue(Map<?, ?> hm, Object value){
    List <Object>list = new ArrayList<Object>();
    for(Object o:hm.keySet()){
        if(hm.get(o).equals(value)) {
            list.add(o);
        }
    }
    return list;
  }
like image 176
Paulius Matulionis Avatar answered Mar 24 '23 07:03

Paulius Matulionis