简体   繁体   English

将Java Map对象转换为Properties对象

[英]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 : 尝试MapAsPropertiesMapAsProperties

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.

相关问题 使用setter将对象转换为Java中的映射 - Converting object with setter to map in java Java反射将属性映射到对象 - Java Reflection to map Properties to Object 将Java Map对象转换为Json数组 - Converting Java Map object to Json array 使用Jackson对象映射器将Map转换为Java对象非常慢 - Converting Map to Java object using Jackson Object Mapper is very slow 使用Java 8流对象将列表对象转换为自定义Map - Converting list object into custom Map using Java 8 stream object 如何使用 Java 8 Streams 分组对象属性并映射到另一个对象? - How to groupBy object properties and map to another object using Java 8 Streams? Java 自定义 Object 具有多个属性作为 Map 键或其属性的串联 - Java Custom Object with multiple properties as Map key or concatenation of its properties 杰克逊将java对象原始属性转换为json字符串中的可选属性 - Jackson converting java object primitive properties as optional in json string 将排序映射从Java转换为Javascript中可读的排序对象 - Converting a Sorted Map from Java to an sorted object readable in Javascript Java 8 Streams:根据不同的属性多次映射同一个对象 - Java 8 Streams: Map the same object multiple times based on different properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM