java – 使用属性文件中的条目填充HashMap
发布时间:2020-12-14 14:26:56 所属栏目:Java 来源:网络整理
导读:我想使用Properties类填充HashMap.我想加载.propeties文件中的条目,然后将其复制到HashMap中. 此前,我曾经使用属性文件初始化HashMap,但是现在我已经定义了HashMap,并希望仅在构造函数中初始化它. 早期方法: Properties properties = new Properties();try
我想使用Properties类填充HashMap.我想加载.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? 解决方法
如果我理解正确,属性中的每个值都是一个代表整数的String.所以代码如下所示:
for (String key : properties.stringPropertyNames()) { String value = properties.getProperty(key); mymap.put(key,Integer.valueOf(value)); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |