【问题标题】:Populating a HashMap with entries from a properties file使用属性文件中的条目填充 HashMap
【发布时间】:2013-05-02 16:01:39
【问题描述】:

我想使用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

【问题讨论】:

    标签: java hashmap


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 如果您在使用 getResourceAsStream 时得到 null,这可能是因为您正在寻找类路径之外的文件。所以要读取绝对文件路径,你可以这样做:File file = new File("absolute/file/path/resume.properties"); FileInputStream fileInputStream = new FileInputStream(file); properties.load(fileInputStream); fileInputStream.close();
      【解决方案3】:

      使用.entrySet()

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

      【讨论】:

        【解决方案4】:
        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;
            }
        

        【讨论】:

        • 你能解释一下吗?
        • 所做的只是读取属性文件,用键和值填充映射。 GetProperty() 只返回带有键和值的 Map。您只需通过 getProperty().get("key") 来访问该值
        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-07
          • 1970-01-01
          • 2021-02-16
          • 2015-03-23
          • 2016-04-30
          • 2017-08-12
          • 2016-09-08
          相关资源
          最近更新 更多