小编典典

转换地图 到地图

java

如何转换 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);

阅读 297

收藏
2020-12-03

共1个答案

小编典典

如果您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()

2020-12-03