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);
/*
* To get all the mappings or entries contained in this hashmap, use
* the entrySet method.
*/
Set> entries = hmap.entrySet();
System.out.println("HashMap entries: " + hmap);
/*
* Changes you make to entry set will be reflected in the
* map, and vice versa.
*/
//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}