JavaRush /Java 博客 /Random-ZH /迭代器。不要忘记+从地图中删除对象
MariaM
第 24 级

迭代器。不要忘记+从地图中删除对象

已在 Random-ZH 群组中发布
我给自己留了一些任务,以便在下一关之后刷新我记忆中的一些主题。迭代器出现问题。让它挂在这里并提醒您如何使用它(如果您突然需要它): 您还可以像这样从映射中删除对象(复制映射以便迭代器不会破坏,并删除原始映射中的所有内容): 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()); }
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION