关于HashMap根据Value获取Key

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jinwufeiyang/article/details/52371959

http://www.cnblogs.com/DreamDrive/p/4673183.html

Map中是一个key有且只有一个value.

但是一个value可以对应多个key值.

一般都是通过key,然后map.get(key)获得到value.

如果想要反向想通过value获得key的值,提供一下两种方法:

方法一:

 1 package cn.itcast.mapgetkey;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 
 7 public class HashMapDemo {
 8     //根据value值获取到对应的一个key值
 9     public static String getKey(HashMap<String,String> map,String value){
10         String key = null;
11         //Map,HashMap并没有实现Iteratable接口.不能用于增强for循环.
12         for(String getKey: map.keySet()){
13             if(map.get(getKey).equals(value)){
14                 key = getKey;
15             }
16         }
17         return key;
18         //这个key肯定是最后一个满足该条件的key.
19     }
20     
21     //根据value值获取到对应的所有的key值
22     public static List<String> getKeyList(HashMap<String,String> map,String value){
23         List<String> keyList = new ArrayList();
24         for(String getKey: map.keySet()){
25             if(map.get(getKey).equals(value)){
26                 keyList.add(getKey);
27             }
28         }
29         return keyList;
30     }
31     
32     public static void main(String[] args) {
33         HashMap<String,String> map = new HashMap();
34         map.put("CHINA", "中国");
35         map.put("CN", "中国");
36         map.put("AM", "美国");
37         //获取一个Key
38         System.out.println("通过value获取Key:"+getKey(map,"中国"));//输出"CN"
39         System.out.println("通过value获取Key:"+getKey(map,"美国"));//输出"AM"
40         //获得所有的key值
41         System.out.println("通过value获取所有的key值:"+getKeyList(map,"中国"));//输出"[CHINA, CN]"
42         
43     }
44 }

方法二:

 1 package cn.itcast.mapgetkey2;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.Iterator;
 6 import java.util.Map;
 7 import java.util.Map.Entry;
 8 import java.util.Set;
 9 
10 public class MapValueGetKey {
11     HashMap<String, String> map = null;
12 
13     public MapValueGetKey(HashMap<String, String> map) {
14         this.map = map;
15     }
16 
17     public static void main(String[] args) {
18         HashMap<String, String> map = new HashMap<String, String>();
19         map.put("1", "a");
20         map.put("2", "b");
21         map.put("3", "c");
22         map.put("4", "c");
23         map.put("5", "e");
24         MapValueGetKey mapValueGetKey = new MapValueGetKey(map);
25         System.out.println(mapValueGetKey.getKey("c"));//输出[3, 4]
26     }
27 
28     private ArrayList<String> getKey(String value) {
29         ArrayList<String> keyList = new ArrayList<String>();
30         String key = null;
31         Set<Entry<String, String>> set = map.entrySet();// entrySet()方法就是把map中的每个键值对变成对应成Set集合中的一个对象.
32         // set对象中的内容如下:[3=c, 2=b, 1=a, 5=e, 4=c]
33         Iterator it = set.iterator();
34         while (it.hasNext()) {
35             Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
36             // entry中的内容就是set集合中的每个对象(map集合中的一个键值对)3=c....
37             // Map.Entry就是一种类型,专值map中的一个键值对组成的对象.
38             if (entry.getValue().equals(value)){
39                 key = (String) entry.getKey();
40                 keyList.add(key);
41             }
42         }
43         return keyList;
44     }
45 }


猜你喜欢

转载自blog.csdn.net/jinwufeiyang/article/details/52371959