【发布时间】:2015-11-18 10:44:33
【问题描述】:
我是 Java 新手,我想为 Properties 对象中的每个键值对调用成员函数,如下所示:
public void addOption(String key, String value)
{
// ...
}
public void foo()
{
Properties properties = new Properties();
// reads the property list from the input byte stream
...
properties.forEach( (key, value) -> this.addOption(key, value)); // compiler error
properties.forEach( (key, value) -> System.out.println("Key: " + key + ": Value: " + value)); // OK
}
报如下编译错误:
The method addOption(String, String) in the type Configuration is not applicable for the arguments (Object, Object)
我做错了什么?
【问题讨论】:
-
key是什么类型?向我们展示定义 -
看来你的
key和value是Object类型,所以只需键入它并尝试。 -
@RossDrew 我已经添加了定义。键和值是
Strings。