繁体   English   中英

如何转换地图 <Object, Object> 到地图 <String, String> 在java中?

[英]How to convert Map<Object, Object> to Map<String, String> in java?

我有Object类型的Map,我需要将此地图转换为String类型。

Map<String, String> map = new HashMap<String, String>();    
Properties properties = new Properties();    
properties.load(instream);     

谁能告诉我,如何将属性分配给上方地图?

谢谢,Msnaidu

Map<String, String> properties2Map(Properties p) {
            Map<String, String> map = new HashMap<String, String>();
            for(Map.Entry<Object, Object> entry : p.entrySet()) {
                String key = (String) entry.getKey(); //not really unsafe, since you just loaded the properties
                map.put(key, p.getProperty(key));
            }
            return map;
}

我还喜欢将实用程序方法与类型参数一起使用,以遍历泛型类型不变性并进行一些“向下转换”或“向上转换”(当我知道这样做是安全的时)。 在这种情况下:

@SuppressWarnings("unchecked")
<A, B extends A> Map<B, B> downCastMap(Map<A,A> map) {
    return (Map<B, B>)map;
}

那你可以写

Properties p = ...
Map<String, String> map = downCastMap(p);

将属性添加到地图的最简单方法是(根据您的示例):

for (String propName : properties.stringPropertyNames()) {
    map.put(propName, properties.getProperty(propName));
}

在这种特殊情况下,这很好用,因为正如getProperty方法清楚地表明, Properties对象实际上是一个包含String键和值的映射。 仅出于可怕的向后兼容性原因Map<Object, Object>才将其声明为Map<Object, Object>

通过使用特定于属性的方法,而不仅仅是将其视为Map<Object, Object> ,您可以使用完美的类型安全性(而不是强制转换)填充Map<String, String>

您可以直接转换:

Properties properties = new Properties();
Map<String, String> map = new HashMap<String, String>((Map)properties);

因为我们知道属性已经是一个字符串到字符串的映射,所以保存它时可以使用rawtype和未经检查的转换。 只需发表评论:

    Properties properties = new Properties();    
    properties.load(instream); 

    @SuppressWarnings({ "rawtypes", "unchecked" })
    // this is save because Properties have a String to String mapping
    Map<String, String> map = new HashMap(properties);

随着Java 8Streams的添加,我建议您使用Steam提供的API

在这里,我们假设每个值实际上都是String对象,对String的强制转换应该是安全的:

Map<String,Object> map = new HashMap<>();

Map<String,String> stringifiedMapSafe = map.entrySet().stream()
     .collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));

现在,以防万一我们不能确定所有元素都是String ,并且我们想用null过滤键/值:

Map<String,Object> map = new HashMap<>();

Map<String,String> stringifiedMapNotSafe = map.entrySet().stream()
             .filter(m -> m.getKey() != null && m.getValue() !=null)
             .collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));
Map<String,String> getPropInMap(Properties prop){       
    Map<String, String> myMap = new HashMap<String, String>();
    for (Object key : prop .keySet()) {
        myMap.put(key.toString(), prop .get(key).toString());
    }       
    return myMap;
}

遍历Map Object,获取kv,将其设置为字符串,然后将其放入Map String。

Map<Object,Object> map1; // with object k,v
Map<String, String> mapString = new HashMap<String, String>();
for (Object key : map1.keySet()) {
                String k = key.toString();
                String v = mmap1.get(key).toString();
                mapString.put(k, v);
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM