轉自: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文件中數據的讀取感覺很好用。