Java HashMap 類的 entrySet 方法傳回此 HashMap 對象中包含的鍵值對的集合。此方法傳回的集合由原對象支援,是以,如果對集合進行任何更改,它将反映在 HashMap 中,反之亦然。
HashMap hmap = new HashMap();
hmap.put(1, "One");
hmap.put(2, "Two");
hmap.put(3, "Three");
System.out.println("HashMap contains: " + hmap);
Set> entries = hmap.entrySet();
System.out.println("HashMap entries: " + hmap);
//remove a mapping from map
hmap.remove(2);
System.out.println("HashMap entries: " + hmap);
輸出:
HashMap contains: {1=One, 2=Two, 3=Three}
HashMap entries: {1=One, 2=Two, 3=Three}
HashMap entries: {1=One, 3=Three}