홈>
LinkedHashMap에서 전체 키/값 쌍을 바꾸려고하는데 작동하지 않는 것 같습니다. 동시 수정 예외가 발생합니다. 주요 변경없이 동일한 위치에서 키의 전체 키/값 쌍을 교체하는 방법이 있습니까?
다음을 시도했습니다 :
Map<String, String> testMap = new LinkedHashMap<String, String>();
testMap.put("1", "One");
testMap.put("2", "Two");
testMap.put("3", "Three");
testMap.put("4", "Four");
testMap.put("5", "Five");
testMap.put("6", "Six");
/*
* Removing an element from the Map.
*/
Iterator<Entry<String, String>> iter = testMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
System.out.print(entry.getKey() + "->" + entry.getValue() + "; ");
if (entry.getKey().equals("1"))
iter.remove(); // remove one entry (key/value) with key "1". Ok
if (entry.getValue().equalsIgnoreCase("Two"))
iter.remove(); // removing all entries (key/value) for value "Two". Ok
if (entry.getKey().equals("3"))
entry.setValue("Updated_Three"); // Updating value for key 3. Ok
// Below how to now add a new key/value pair in my testMap at this position
// without affecting any other order?
if (entry.getKey().equals("4")) {
iter.remove();
// How to now add a new key/value pair at this position without affecting any
// other order.
// testMap.put("44", "FourFour"); // Removing entry with key 4 and adding a new
// key/valuepair to hashmap. It throws ConcurrentModificationException. Any
// other way to perform this?
}
}
- 답변 # 1
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- JavaScript 변수를 HTML div에 '출력'하는 방법
- python - 문자열에서 특정 문자 제거
내가 아는 한, LinkedHashMap을 고수하고 싶다. 이 과정을 반복하면서 새 데이터를 추가 (LinkedHashMap의 구조 변경)하려고하면 ConcurrentModificationException이 발생합니다.
다음 코드는 요구 사항을 충족시킬 수 있습니다.
출력 :
와이즈 비즈 LinkedHashMap의 특정 인덱스에 요소를 추가하기위한 코드 여기에서