1. 程式人生 > >map集合根據value找key(一個key或多個key)

map集合根據value找key(一個key或多個key)

   //根據value值獲取到對應的一個key值
    public static String getKey(HashMap<String,String> map,String value){
        String key = null;
        //Map,HashMap並沒有實現Iteratable介面.不能用於增強for迴圈.
        for(String getKey: map.keySet()){
            if(map.get(getKey).equals(value)){
                key = getKey;
            }
        }
        return
key; //這個key肯定是最後一個滿足該條件的key. } //根據value值獲取到對應的所有的key值 public static List<String> getKeyList(HashMap<String,String> map,String value){ List<String> keyList = new ArrayList(); for(String getKey: map.keySet()){ if(map.get(getKey).equals(value)){ keyList.
add(getKey); } } return keyList; }