[英]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? 是否有人能够为我提供比下面更好的方法将Java Map对象转换为Properties对象?
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 谢谢
Use Properties::putAll(Map<String,String>)
method: 使用
Properties::putAll(Map<String,String>)
方法:
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key", "value");
Properties properties = new Properties();
properties.putAll(map);
you also can use apache commons-collection4
你也可以使用apache
commons-collection4
org.apache.commons.collections4.MapUtils#toProperties(Map<K, V>)
example: 例:
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("name", "feilong");
map.put("age", "18");
map.put("country", "china");
Properties properties = org.apache.commons.collections4.MapUtils.toProperties(map);
see javadoc 见javadoc
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MapUtils.html#toProperties(java.util.Map) https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MapUtils.html#toProperties(java.util.Map)
You can do this with Commons Configuration: 您可以使用Commons Configuration执行此操作:
Properties props = ConfigurationConverter.getProperties(new MapConfiguration(map));
http://commons.apache.org/configuration http://commons.apache.org/configuration
Try MapAsProperties
from Cactoos : 尝试
MapAsProperties
的MapAsProperties :
import org.cactoos.list.MapAsProperties;
import org.cactoos.list.MapEntry;
Properties pros = new MapAsProperties(
new MapEntry<>("foo", "hello, world!")
new MapEntry<>("bar", "bye, bye!")
);
Simple use putAll()
简单使用
putAll()
Properties pro = new Properties();
pro.putAll(myMapObject);
As it accepts Map as input. 因为它接受Map作为输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.