JavaRush /Java Blog /Random-TW /迭代器。不要忘記+從地圖中刪除對象
MariaM
等級 24

迭代器。不要忘記+從地圖中刪除對象

在 Random-TW 群組發布
我給自己留了一些任務,以便在下一關之後刷新我記憶中的一些主題。迭代器出現問題。讓它掛在這裡並提醒您如何使用它(如果您突然需要它): 您也可以像這樣從映射中刪除物件(複製映射以便迭代器不會破壞,並刪除原始映射中的所有內容): 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