简体   繁体   English

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

[英]Populating a HashMap with entries from a properties file

I want to populate a HashMap using the Properties class.我想使用Properties类填充HashMap
I want to load the entries in the .propeties file and then copy it into the HashMap .我想加载.propeties文件中的条目,然后将其复制到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.之前我只是用属性文件来初始化HashMap ,但是现在我已经定义了HashMap ,只想在构造函数中初始化它。

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?如何在此处将属性对象分配给HashMap

If I understand correctly, each value in the properties is a String which represents an Integer.如果我理解正确,属性中的每个值都是一个代表整数的字符串。 So the code would look like this:所以代码看起来像这样:

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

Use .entrySet()使用.entrySet()

for (Entry<Object, Object> entry : properties.entrySet()) {
    map.put((String) entry.getKey(), (String) entry.getValue());
}

Java 8 Style: Java 8 风格:

Properties properties = new Properties();
// add  some properties  here
Map<String, String> map = new HashMap();

map.putAll(properties.entrySet()
                     .stream()
                     .collect(Collectors.toMap(e -> e.getKey().toString(), 
                                               e -> e.getValue().toString())));
public static Map<String,String> getProperty()
    {
        Properties prop = new Properties();
        Map<String,String>map = new HashMap<String,String>();
        try
        {
            FileInputStream inputStream = new FileInputStream(Constants.PROPERTIESPATH);
            prop.load(inputStream);
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println("Some issue finding or loading file....!!! " + e.getMessage());

        }
        for (final Entry<Object, Object> entry : prop.entrySet()) {
            map.put((String) entry.getKey(), (String) entry.getValue());
        }
        return map;
    }

you can use the below Method to load the property file in a map您可以使用以下方法在地图中加载属性文件

public static Map<String, String> getProperties(String filePropertyFile){
        Properties getProperties = new Properties();
        FileInputStream inputStream = null;
        HashMap<String, String> propertyMap = new HashMap<String, String>();
        try {
            inputStream = new FileInputStream(filePropertyFile);
            getProperties.load(inputStream);
            propertyMap.putAll((Map) getProperties);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return propertyMap;
    }

Unit Test:单元测试:

@Test
public void getPropertiesTest()
    String outputOmJarArrayAttributesPath = System.getProperty("user.dir") + "/src/main/resources/OutputOmJarArrayAttributes.properties";
    System.out.println(PropertyFileUtils.getProperties(outputOmJarArrayAttributesPath));;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM