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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM