将java.util.Properties转换为HashMap< string,string> [英] Converting java.util.Properties To HashMap<string,string>
本文介绍了将java.util.Properties转换为HashMap< string,string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Properties properties = new Properties();
Map<String, String> map = new HashMap<String, String>(properties);// why wrong?
java.util.Properties是Map的一个实现,而HashMap构造函数接收一个Map类型的param。
但为什么必须明确转换?
java.util.Properties is a implement of Map, And HashMap constructor receive a Map type param. But why must convert explicitly?
推荐答案
这是因为 属性
extends Hashtable< Object,Object>
(其中反过来,实现 Map< Object,Object>
)。您尝试将其提供给 Map< String,String>
。因此它是不兼容的。
This is because Properties
extends Hashtable<Object, Object>
(which, in turn, implements Map<Object, Object>
). You attempt to feed that into a Map<String, String>
. It is therefore incompatible.
您需要将字符串属性逐个输入到地图中...
You need to feed string properties one by one into your map...
例如:
for (final String name: properties.stringPropertyNames())
map.put(name, properties.getProperty(name));
这篇关于将java.util.Properties转换为HashMap< string,string>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文