Search code examples
javaimmutabilitytreemap

Why java TreeMap.entrySet returns a set of SimpleImmutableEntry?


I have a java application installed on 2 different servers running with java 1.6.0_22-b04 sun and tomcat 6.0.32

The staging server is causing me headaches with the following error :

java.lang.UnsupportedOperationException : null
   java.util.AbstractMap$SimpleImmutableEntry.setValue (AbstractMap.java:726)

Here's the code raising this exception

for (Entry<Integer, BigDecimal> entry : map.entrySet()) {
  entry.setValue(new BigDecimal("999"));
}

I'm only having this problem on this server. Everything is fine locally and on the other server with the same code.

I found lots of results on google about this exception but they were all linked to hudson.


Solution

  • From the TreeMap Javadoc in Java 6:

    All Map.Entry pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support the Entry.setValue method. (Note however that it is possible to change mappings in the associated map using put.)

    So -- why are you even surprised?