Java – populate the HashMap with entries in the properties file

I want to use the properties class to populate the HashMap I want to load The entries in the propeties file, and then copy them to the HashMap

Previously, I used to initialize the HashMap using the properties file, but now I have defined the HashMap and want to initialize it only in the constructor

Early methods:

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 to assign attribute objects to HashMap here?

Solution

If I understand it correctly, each value in the attribute is a string representing an integer So the code is as follows:

for (String key : properties.stringPropertyNames()) {
    String value = properties.getProperty(key);
    mymap.put(key,Integer.valueOf(value));
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>