JavaRush /Java Blog /Random EN /Iterator. Not to forget + removing objects from map
MariaM
Level 24

Iterator. Not to forget + removing objects from map

Published in the Random EN group
I left myself a few tasks to refresh some topics in my memory after the next levels. There was a problem with the iterator. Let it hang here and remind you how to work with it (if you suddenly need it): And you can also delete objects from the map like this (copy the map so that the iterator does not break, and delete everything in the original map): 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()); }
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION