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