How can I convert Map
to Map
?
This does not work:
Map map = ne
There are two ways to do this. One is very simple but unsafe:
Map map = new HashMap();
Map newMap = new HashMap((Map)map); // unchecked warning
The other way has no compiler warnings and ensures type safety at runtime, which is more robust. (After all, you can't guarantee the original map contains only String values, otherwise why wouldn't it be Map
in the first place?)
Map map = new HashMap();
Map newMap = new HashMap();
@SuppressWarnings("unchecked") Map intermediate =
(Map)Collections.checkedMap(newMap, String.class, String.class);
intermediate.putAll(map);