次のレベルの後、記憶にあるいくつかのトピックをリフレッシュするために、いくつかのタスクを自分に残しました。イテレータに問題がありました。ここにハングさせて、(突然必要になった場合に) 操作方法を思い出させます。 また、次のようにマップからオブジェクトを削除することもできます (反復子が壊れないようにマップをコピーし、元のマップ内のすべてを削除します) ):
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