码迷,mamicode.com
首页 > 其他好文 > 详细

HashMap根据value获取key值

时间:2017-09-09 15:49:18      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:family   set   ash   获取   一个   重复   turn   entryset   add   

public static String getCityId(HashMap<String,String> citys, String city){
Set set = citys.entrySet();//新建一个不可重复的集合
ArrayList<String> arr = new ArrayList<String>();//新建一个集合
Iterator it = set.iterator();//遍历的类
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();//找到所有key-value对集合
if(entry.getValue().equals(city)) {//通过判断是否有该value
String s = (String) entry.getKey();//取得key
arr.add(s);
}
}
return arr.get(0);
}

HashMap根据value获取key值

标签:family   set   ash   获取   一个   重复   turn   entryset   add   

原文地址:http://www.cnblogs.com/kim-liu/p/7498363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!