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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16342637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:24:37  来源:igfitidea点击:

Populating a HashMap with entries from a properties file

javahashmap

提问by OneMoreError

I want to populate a HashMapusing the Propertiesclass.
I want to load the entries in the .propetiesfile and then copy it into the HashMap.

我想HashMap使用Properties该类填充 a 。
我想加载.propeties文件中的条目,然后将其复制到HashMap.

Earlier, I used to just initialize the HashMapwith the properties file, but now I have already defined the HashMapand 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 HashMaphere?

如何将属性对象分配给HashMap此处?

采纳答案by JB Nizet

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));
}

回答by cahen

Use .entrySet()

.entrySet()

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

回答by Rupesh Patil

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;
    }

回答by aprodan

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())));