如何将所有 Java 系统属性转换为 HashMap<String,String>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17331543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:41:58  来源:igfitidea点击:

How to convert all Java System Properties to HashMap<String,String>?

javahashmapsystem-properties

提问by

This nice articleshows us how to print all the current system properties to STDOUT, but I need to convert everything that's in System.getProperties()to a HashMap<String,String>.

这篇不错的文章向我们展示了如何将所有当前系统属性打印到 STDOUT,但我需要将所有内容转换System.getProperties()HashMap<String,String>.

Hence if there is a system property called "baconator", with a value of "yes!", that I set with System.setProperty("baconator, "yes!"), then I want the HashMapto have a key of baconatorand a respective value of yes!, etc. Same idea for allsystem properties.

因此,如果有一个名为“baconator”的系统属性,其值为“yes!”,我用 设置System.setProperty("baconator, "yes!"),那么我希望HashMap有一个键baconator和一个相应的值yes!,等等。所有系统属性的想法都是一样的。

I tried this:

我试过这个:

Properties systemProperties = System.getProperties();
for(String propertyName : systemProperties.keySet())
    ;

But then get an error:

但随后得到一个错误:

Type mismatch: cannot convert from element type Object to String

类型不匹配:无法从元素类型对象转换为字符串

So then I tried:

然后我尝试了:

Properties systemProperties = System.getProperties();
for(String propertyName : (String)systemProperties.keySet())
    ;

And am getting this error:

并且收到此错误:

Can only iterate over an array or an instance of java.lang.Iterable

只能迭代数组或 java.lang.Iterable 的实例

Any ideas?

有任何想法吗?

采纳答案by Luiggi Mendoza

I did a sample test using Map.Entry

我做了一个样本测试使用 Map.Entry

Properties systemProperties = System.getProperties();
for(Entry<Object, Object> x : systemProperties.entrySet()) {
    System.out.println(x.getKey() + " " + x.getValue());
}

For your case, you can use this to store it in your Map<String, String>:

对于您的情况,您可以使用它来将其存储在您的Map<String, String>

Map<String, String> mapProperties = new HashMap<String, String>();
Properties systemProperties = System.getProperties();
for(Entry<Object, Object> x : systemProperties.entrySet()) {
    mapProperties.put((String)x.getKey(), (String)x.getValue());
}

for(Entry<String, String> x : mapProperties.entrySet()) {
    System.out.println(x.getKey() + " " + x.getValue());
}

回答by rgettman

Loop over the Set<String>(which is Iterable) that is returned by the stringPropertyNames()method. When processing each property name, use getPropertyto get the property value. Then you have the information needed to putyour property values into your HashMap.

循环遍历该方法返回的Set<String>(即IterablestringPropertyNames()。处理每个属性名称时,使用getProperty来获取属性值。然后您将put您的属性值所需的信息放入您的HashMap.

回答by Agustí Sánchez

Since Java 8, you can type this -rather long- one-liner:

从 Java 8 开始,你可以输入这个——相当长的——单行:

Map<String, String> map = System.getProperties().entrySet().stream()
  .collect(Collectors.toMap(e -> (String) e.getKey(), e -> (String) e.getValue()));

回答by darijan

This does work

这确实有效

Properties properties= System.getProperties();
for (Object key : properties.keySet()) {
    Object value= properties.get(key);

    String stringKey= (String)key;
    String stringValue= (String)value;

    //just put it in a map: map.put(stringKey, stringValue);
    System.out.println(stringKey + " " + stringValue);
}

回答by JHS

Either you can use the entrySet()method from Propertiesto get an Entrytype from Propertieswhich is Iterableor you could use the stringPropertyNames()method from the Propertiesclass to get a Setof keys in this property list. The use the getPropertymethod to get the property value.

您可以使用entrySet()from 方法Properties获取is的Entry类型,PropertiesIterable可以使用类中的stringPropertyNames()方法Properties获取Set此属性列表中的a键。使用getProperty方法获取属性值。