使用属性文件中的条目填充HashMap


问题内容

我想HashMap使用Properties该类填充一个。
我想将条目加载到.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此处?


问题答案:

如果我理解正确,则属性中的每个值都是代表整数的字符串。因此,代码如下所示:

for (String key : properties.stringPropertyNames()) {
    String value = properties.getProperty(key);
    mymap.put(key, Integer.valueOf(value));
}