I am trying to replace whole key/value pair from my LinkedHashMap but seems it is not working. I am getting concurrentmodificationexception. Is there a way to replace whole key/value pair for a key at the same position without any major changes.
I tried to do the following:
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? } }
Advertisement
Answer
As far as I have understood, you want to stick with LinkedHashMap. If you will try to add new data (change the structure of the LinkedHashMap) while iterating over this, you will get ConcurrentModificationException.
Below code might fulfill your requirement :
public static void main(String args[]) throws IOException { Map<String, String> testMap = new LinkedHashMap<>(); testMap.put("1", "One"); testMap.put("2", "Two"); testMap.put("3", "Three"); testMap.put("4", "Four"); int indexOfFourtyFour = -1; System.out.println("Test Map before :: " + testMap); Iterator<Entry<String, String>> itr = testMap.entrySet().iterator(); int index = 0; while (itr.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) itr.next(); if (entry.getKey().equals("3")) { itr.remove(); indexOfFourtyFour = index; } index ++; } if (indexOfFourtyFour > -1) { add(testMap, indexOfFourtyFour, "44", "FourFour"); } System.out.println("Test Map after :: " + testMap); } public static <K, V> void add(Map<K, V> map, int index, K key, V value) { assert (map != null); assert !map.containsKey(key); assert (index >= 0) && (index < map.size()); int i = 0; List<Entry<K, V>> rest = new ArrayList<Entry<K, V>>(); for (Entry<K, V> entry : map.entrySet()) { if (i++ >= index) { rest.add(entry); } } map.put(key, value); for (int j = 0; j < rest.size(); j++) { Entry<K, V> entry = rest.get(j); map.remove(entry.getKey()); map.put(entry.getKey(), entry.getValue()); } }
Output :
Test Map before :: {1=One, 2=Two, 3=Three, 4=Four}
Test Map after :: {1=One, 2=Two, 44=FourFour, 4=Four}
Code for add element at specific index in LinkedHashMap from HERE