我有一張地圖,我需要獲取特定的鍵和值。我試過使用 for 循環,但這似乎并不能解決我的問題。Map<Integer, String> map = new HashMap<>();
map.put(0, "$");
map.put(0, "|");
map.put(0, "*");我需要獲取特定項目的鍵和值。例如,我只需要獲取 的鍵和值money,而不需要其他任何內容。
1 回答

RISEBY
TA貢獻1856條經驗 獲得超5個贊
Map 不能包含重復的鍵,但可以包含重復的值。
Map<Integer, String> map = new HashMap<>();
map.put(0, "$");
map.put(1, "|");
map.put(2, "*");
for(Map.Entry<Integer, String> m: map.entrySet()) {
if(m.getValue().equals("$")) {
System.out.println(m.getKey() + ":" + m.getValue());
}
}
輸出 :
0:$
添加回答
舉報
0/150
提交
取消