24

我想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此处?

4

5 回答 5

33

如果我理解正确,属性中的每个值都是代表整数的字符串。所以代码看起来像这样:

for (String key : properties.stringPropertyNames()) {
    String value = properties.getProperty(key);
    mymap.put(key, Integer.valueOf(value));
}
于 2013-05-02T16:07:10.143 回答
25

采用.entrySet()

for (Entry<Object, Object> entry : properties.entrySet()) {
    map.put((String) entry.getKey(), (String) entry.getValue());
}
于 2013-05-02T16:19:21.533 回答
12

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())));
于 2017-02-14T00:51:57.360 回答
1
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;
    }
于 2017-01-02T18:10:20.450 回答
0

您可以使用以下方法在地图中加载属性文件

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

单元测试:

@Test
public void getPropertiesTest()
    String outputOmJarArrayAttributesPath = System.getProperty("user.dir") + "/src/main/resources/OutputOmJarArrayAttributes.properties";
    System.out.println(PropertyFileUtils.getProperties(outputOmJarArrayAttributesPath));;
    }
于 2020-10-03T20:46:37.320 回答