关于HashMap根据Value获取Key

Map 中是一个 key有且只有一个value,但是一个value可以对应多个key值,一般都是通过key,然后map.get(key)获得value,如果想要反向value获得key的值,提供以下两种方法:

方法一:

public class HashMapDemo {
    //根据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;
    }

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap();
        map.put("CHINA", "中国");
        map.put("CN", "中国");
        map.put("AM", "美国");
        //获取一个Key
        System.out.println("通过value获取Key:" + getKey(map, "中国"));//输出"CN"
        System.out.println("通过value获取Key:" + getKey(map, "美国"));//输出"AM"
        //获得所有的key值
        System.out.println("通过value获取所有的key值:" + getKeyList(map, "中国"));//输出"[CHINA, CN]"

    }

}

输出结果:

通过value获取Key:CN
通过value获取Key:AM
通过value获取所有的key值:[CHINA, CN]

方法二

import java.util.*;
import java.util.Map.Entry;

public class MapValueGetKey {
    HashMap<String,String> map = null;

    public MapValueGetKey(HashMap<String,String> map){
        this.map= map;
    }

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "a");
                map.put("2", "b");
                map.put("3", "c");
                map.put("4", "c");
                map.put("5", "e");
                MapValueGetKey mapValueGetKey = new MapValueGetKey(map);
                System.out.println(mapValueGetKey.getKey("c"));//输出[3, 4]

    }
    private ArrayList<String> getKey(String value) {
             ArrayList<String> keyList = new ArrayList<String>();
             String key = null;
             Set<Entry<String, String>> set = map.entrySet();// entrySet()方法就是把map中的每个键值对变成对应成Set集合中的一个对象.
             // set对象中的内容如下:[3=c, 2=b, 1=a, 5=e, 4=c]
             Iterator it = set.iterator();
             while (it.hasNext()) {
                     Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
                     // entry中的内容就是set集合中的每个对象(map集合中的一个键值对)3=c....
                     // Map.Entry就是一种类型,专值map中的一个键值对组成的对象.
                     if (entry.getValue().equals(value)){
                           key = (String) entry.getKey();
                           keyList.add(key);
                       }
                 }
             return keyList;
         }

}

输出结果:

[3, 4]

版权声明:本文为m0_47073109原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。