簡體   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