JavaRush /Java Blog /Random-KO /반복자. 잊지 마세요 + 지도에서 객체 제거
MariaM
레벨 24

반복자. 잊지 마세요 + 지도에서 객체 제거

Random-KO 그룹에 게시되었습니다
다음 레벨 이후에 기억에 남는 몇 가지 주제를 새로 고치기 위해 몇 가지 작업을 맡겼습니다. 반복자에 문제가 발생했습니다. 여기에 걸어두고 작업 방법을 상기시켜 줍니다(갑자기 필요한 경우). 그리고 다음과 같이 맵에서 객체를 삭제할 수도 있습니다(반복자가 깨지지 않도록 맵을 복사하고 원본 맵의 모든 것을 삭제합니다) ): 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