Search code examples
javaconcurrentmodificationmultivalue

Changing MultivalueMap keys throws ConcurrentModificationException


I have MultivalueMap and a TreeMap, both have key-value of type String.

I wish to change any key in my MultivalueMap to keys found in my TreeMap.

This is my code, why do I get ConcurrentModificationException and how to solve it?

public RestRequest(MultivaluedMap<String, Object> myHeaders,TreeMap<String, String> testParameters) {
    this.testParameters = testParameters;
    buildUrl();     
    Client client = ClientBuilder.newClient();

    Set<String> keys = myHeaders.keySet();
    for (String key : keys) {
        myHeaders.remove(key);
        myHeaders.add(key, testParameters.get(key));
    }
    this.myHeaders = myHeaders;
    myResource = client.target(URL);
    entity=Entity.entity(replaceRequestBodyWithParams(requestBody), MediaType.APPLICATION_JSON);
}  

Solution

  • You can't modify the underlying collection that you're iterating over. You are looping over the keySet of a map and in the loop try to change that map, which would also alter the keySet.

    A better way to do achieve what you're trying to do in this method, would be to construct a new data structure like this:

    public RestRequest(MultivaluedMap<String, Object> myHeaders,TreeMap<String, String> testParameters) {
        this.testParameters = testParameters;
        buildUrl();     
        Client client = ClientBuilder.newClient();
    
        Set<String> keys = myHeaders.keySet();
        final Map<String, String> testParamsByHeader = new HashMap<>();
    
        for (String key : keys)
            testParamsByHeader.add(key, testParameters.get(key));
        }
        this.myHeaders = testParamsByHeader;
        myResource = client.target(URL);
        entity=Entity.entity(replaceRequestBodyWithParams(requestBody), MediaType.APPLICATION_JSON);
    }