HashMap根据value获取key值

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);
}
原文地址:https://www.cnblogs.com/kim-liu/p/7498363.html