Question: Hi  

Is the class below threadsafe?

```
class ImmutablePossiblyThreadsafeClass<K, V> {

    private final Map<K, V> map;

    public ImmutablePossiblyThreadsafeClass(final Map<K, V> map) {
        this.map = new HashMap<K, V>();

        for (Entry<K, V> entry : map.entrySet()) {
            this.map.put(entry.getKey(), entry.getValue());
        }
    }

    public V get(K key) {
        return this.map.get(key);
    }
}

```

Answer: 
If this is the whole class definition, i.e. there are no other setter/modifier methods, the usage of the map itself is thread safe:

* its initialization and visibility is safeguarded by declaring it `final`,
* the containing class has only a getter, no modifier methods,
* the contents of the map are copied from the constructor parameter map, so there can be no outside references through which it can be modified.

(Note though that this refers to the structure of the map only - the individual elements inside the map may still be unsafe.)

### Update

Regarding the claim that the class is not guarded from the constructor parameter being concurrently modified during construction, I tend to agree with others (incl. @Bozho) who have already pointed out that

1. there is no known way to guard against this (not even using a `ConcurrentHashMap` as @Grundlefleck suggested - I checked the source code and its constructor just calls `putAll()`, which is not guarded against this either),
2. it is the caller's responsibility rather than the callee's to ensure that the constructor parameter is not being concurrently modified. In fact, can anyone define the "right" behaviour for processing a constructor parameter which is being concurrently modified? Should the created object contain the original elements in the parameter map, or the latest elements?... If one starts to think about this, IMHO it is not difficult to realize that the only consistent solution is to disallow concurrent modification of the constructor parameter, and this can only be ensured by the caller.
Yes, it is thread-safe. It is immutable so no thread can make any changes that will interfere with the results for another thread.

Note: If the map is modified during construction, you won't get the contents you thought you passed, but when accessing the map afterwards, it will be thread-safe.