0

Is it equivalent between

return ImutableMap.copyOf(map);

and

return Collections.unmodifiableMap(new HashMap(map));
return Collections.unmodifiableMap(new LinkedHashMap(map));

And is second way thread-safe?

(ImutableMap from Guava, I try second way because ImmutableMap doesn't support null-value)

FredSuvn
  • 1,869
  • 2
  • 12
  • 19

1 Answers1

2

The only difference b/w the two is an unmodified map will get updated if the map it is backing is updated.

While an immutable map is truly immutable.

You create an unmodified map, which will behave similar to Immutable map.

Map<String, String> immutableMap = 
Collections.unmodifiableMap(new HashMap<String, String>(map)); 

Both are thread safe in sense as you are restricting update.

ihimanshu19
  • 136
  • 1
  • 8