I want to populate a HashMap
using the Properties
class.
I want to load the entries in the .propeties
file and then copy it into the HashMap
.
Earlier, I used to just initialize the HashMap
with the properties file, but now I have already defined the HashMap
and want to initialize it in the constructor only.
Earlier approach:
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);
But now, I have this
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
}
}
How do I assign the properties object to a HashMap
here?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…