如何转换 Map<String,Object>为Map<String,String>?
Map<String,Object>
Map<String,String>
这不起作用:
Map<String,Object> map = new HashMap<String,Object>(); //Object is containing String Map<String,String> newMap =new HashMap<String,String>(map);
如果您Objects只包含Strings,则可以这样操作:
Objects
Strings
Map<String,Object> map = new HashMap<String,Object>(); //Object is containing String Map<String,String> newMap =new HashMap<String,String>(); for (Map.Entry<String, Object> entry : map.entrySet()) { if(entry.getValue() instanceof String){ newMap.put(entry.getKey(), (String) entry.getValue()); } }
如果Objects不是,String则可以替换 (String) entry.getValue()为entry.getValue().toString()。
String
(String) entry.getValue()
entry.getValue().toString()