hashmap通过value获取key值

版权声明:本博文仅供学习、参考、技术讨论,版权归笔者/译者所有。 https://blog.csdn.net/qq_38025219/article/details/83060269

前言:此方法用于value值为唯一的情况。

//根据value值获取到对应的一个key值
     public static String getKey(LinkedHashMap<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.
     }

猜你喜欢

转载自blog.csdn.net/qq_38025219/article/details/83060269
今日推荐