关于HashMap根据Value获取Key

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

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

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

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

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

方法一:

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

方法二:

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值