【发布时间】:2013-05-02 16:01:39
【问题描述】:
我想使用Properties 类填充HashMap。
我想加载.propeties文件中的条目,然后将其复制到HashMap中。
之前,我只是用属性文件初始化HashMap,但现在我已经定义了HashMap,只想在构造函数中初始化它。
较早的方法:
Properties properties = new Properties();
try {
properties.load(ClassName.class.getResourceAsStream("resume.properties"));
} catch (Exception e) {
}
HashMap<String, String> mymap= new HashMap<String, String>((Map) properties);
但现在,我有这个
public class ClassName {
HashMap<String,Integer> mymap = new HashMap<String, Integer>();
public ClassName(){
Properties properties = new Properties();
try {
properties.load(ClassName.class.getResourceAsStream("resume.properties"));
} catch (Exception e) {
}
mymap = properties;
//The above line gives error
}
}
如何在此处将属性对象分配给HashMap?
【问题讨论】: