1
If you only want it to iterate over your HashMap, no need for a list:
如果您只想讓它迭代您的HashMap,則不需要列表:
HashMap map = new HashMap();
map.put (1, "Mark");
map.put (2, "Tarryn");
for (String s : map.values()) {
System.out.println(s);
}
Of course, if you want to modify your map structurally (i.e. more than only changing the value for an existing key) while iterating, then you better use the "copy to ArrayList" method, since otherwise you'll get a ConcurrentModificationException. Or export as an array:
當然,如果你想在迭代時在結構上修改你的地圖(即不僅僅是改變現有鍵的值),那么你最好使用“copy to ArrayList”方法,否則你將得到一個ConcurrentModificationException。或導出為數組:
HashMap map = new HashMap();
map.put (1, "Mark");
map.put (2, "Tarryn");
for (String s : map.values().toArray(new String[]{})) {
System.out.println(s);
}