JavaHashmap:如何從值中獲取密鑰?如果我有這個價值"foo",以及HashMap<String> ftw對此ftw.containsValue("foo")回報true如何獲得相應(yīng)的密鑰?我必須遍歷hashmap嗎?最好的方法是什么?
3 回答

白衣非少年
TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個贊
public class NewClass1 { public static void main(String[] args) { Map<Integer, String> testMap = new HashMap<Integer, String>(); testMap.put(10, "a"); testMap.put(20, "b"); testMap.put(30, "c"); testMap.put(40, "d"); for (Entry<Integer, String> entry : testMap.entrySet()) { if (entry.getValue().equals("c")) { System.out.println(entry.getKey()); } } }}
一些額外的信息.??赡軐δ阌杏?/strong>
1. Key to value2. Value to key
添加回答
舉報
0/150
提交
取消