我正在尝试执行以下操作
interface Updater {
void update(String value);
}
void update(Collection<String> values, Updater updater) {
update(values, updater::update, 0);
}
void update(Collection<String> values, Function<String, Void> fn, int ignored) {
// some code
}
但我收到此编译器错误:
"Cannot convert void to java.lang.Void"
这意味着updater::update
不能用作Function<String, Void>
。
当然我不会写Function <String, void>
,也不想将返回类型更改update()
为Void
。
我该如何解决这个问题?
Function
即使返回声明为类型的值,A也会返回一个值Void
(您必须先返回null
。相反,一个void
方法实际上什么也不会返回,甚至不会返回null
。因此,您必须插入以下return
语句:
void update(Collection<String> values, Updater updater) {
update(values, s -> { updater.update(); return null; }, 0);
}
一种替代方法是将更Function<String,Void>
改为Consumer<String>
,然后可以使用方法参考:
void update(Collection<String> values, Updater updater) {
update(values, updater::update, 0);
}
void update(Collection<String> values, Consumer<String> fn, int ignored) {
// some code
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句