【问题标题】:properties to hashmap<String, String> [duplicate]hashmap<String, String> 的属性 [重复]
【发布时间】:2015-05-14 01:39:19
【问题描述】:

我正在尝试将属性文件中的所有值重写为哈希映射,但是当我尝试运行此代码时

for (String keys: properties.entrySet())
    {
        hMap.put(keys,  properties.get(keys));
    }

我收到以下错误。

The method put(String, String) in the type Map<String,String> is not applicable for the arguments (Map.Entry<Object,Object>, Object)

我知道一个是字符串类型,一个是对象,但我不知道如何解决它,因为我对编程很陌生...

【问题讨论】:

  • 是的,这是一个重复的问题......我已经尝试解决这个问题一段时间了,这个线程帮助很大,更大的问题是从文件中读取属性而不是把它们变成哈希图....再次感谢您,抱歉重复...

标签: java properties hashmap


【解决方案1】:

您将 Map.Entry 作为 Properties.entrySet() 的返回类型。

for (Map.Entry entry: properties.entrySet(
{
   hMap.put((String)entry.getKey(),  (String)entry.getValue());
}

【讨论】:

    【解决方案2】:

    这应该适合你:

    for (String keys: properties.stringPropertyNames())  
    {
            hMap.put(keys,  properties.get(keys));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2011-04-07
      • 2013-07-29
      相关资源
      最近更新 更多