我给自己留了一些任务,以便在下一关之后刷新我记忆中的一些主题。迭代器出现问题。让它挂在这里并提醒您如何使用它(如果您突然需要它): 您还可以像这样从映射中删除对象(复制映射以便迭代器不会破坏,并删除原始映射中的所有内容):
public static void removeAllSummerPeople(HashMap
map) { //write your code here Iterator
> iterator=map.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry
pair=iterator.next(); int month=pair.getValue().getMonth(); if(month>=5&&month<8){ iterator.remove(); } } }
Map
map = new HashMap<>(); map.put("1", "2"); map.put("3", "4"); Map
mapCopy = new HashMap<>(map); for(Map.Entry
pair : mapCopy.entrySet()) { map.remove(pair.getKey()); }
GO TO FULL VERSION