转自: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文件中数据的读取感觉很好用。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
猜你喜欢
  • 2021-08-06
  • 2021-10-28
  • 2021-10-01
  • 2022-01-22
  • 2022-12-23
  • 2021-05-10
相关资源
相似解决方案