Properties 轉換成Map


轉自:http://feitianbenyue.iteye.com/blog/1759259

對於Properties 轉換成Map 的問題:

第一時間想到的肯定有以下:
1.  迭代出來  再 put 到 map 中去;
2. commons 是否有工具類 ;
 
可是 由於 Properties  實現了Map 接口,  所以有最最簡單的 ,強制轉換:
package com.feilong.example.util;

import java.util.Properties;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

public class PropertiesToMap {
    public static void main(String[] args) {

        Properties properties = new Properties();


        properties.setProperty("StrictHostKeyChecking", "no");
        properties.setProperty("app.version", "1.0");

        // Create a new HashMap and pass an instance of Properties. Properties
        // is an implementation of a Map which keys and values stored as in a string.
        Map<String, String> map = new HashMap<String, String>((Map) properties);


        // Get the entry set of the Map and print it out.

        Set propertySet = map.entrySet();
        for (Object o : propertySet) {
            Map.Entry entry = (Map.Entry) o;
            System.out.printf("%s = %s%n", entry.getKey(), entry.getValue());
        }
    }
}

對於**.properties文件中數據的讀取感覺很好用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM