Joel Joel - 5 months ago 20
Java Question

Converting a Java Map object to a Properties object

Is anyone able to provide me with a better way than the below for converting a Java Map object to a Properties object?

Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key", "value");

Properties properties = new Properties();

for (Map.Entry<String, String> entry : map.entrySet()) {
properties.put(entry.getKey(), entry.getValue());
}


Thanks

Answer

Use Properties.putAll(Map) method:

Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key", "value");

Properties properties = new Properties();
properties.putAll(map);