[英]Sfl4j and java 8: The target type of this expression must be a functional interface
[英]The target type of this expression must be a functional interface in java
lmod.forEachWithIndex( (e, i) -> lmod.set(i, e*2));
我收到错误消息:
XList类型的forEachWithIndex(Object)方法不适用于参数((e,i)-> {})此表达式的目标类型必须是功能接口
这是什么意思? 我该如何解决?
import java.util.*;
public class Main {
public static void main(String[] args) {
XList<Integer> lmod = XList.of( new Integer[] {1,2,8, 10, 11, 30, 3, 4});
lmod.forEachWithIndex( (e, i) -> lmod.set(i, e*2));
System.out.println(lmod);
}}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
public class XList implements Collection, Consumer {
public List<T> list = new ArrayList<T>();
public XList(T[] ints) {
for( int i = 0; i < ints.length; i++)
list.add(ints[i]);
}
public static XList<Integer> of(Integer[] ints) {
return new XList(ints);
}
public void forEachWithIndex(Object object) {
}
@Override
public void accept(T t) {
}
}
我添加了功能接口Consumer,但我仍然不知道下一步该怎么做
您可以通过以下代码绕过错误,但我强烈建议您将forEachWithIndex参数从Object
修改为BiConsumer
喜欢
public void forEachWithIndex(BiConsumer biConsumer) {}
如果您仍然想严格执行,这里的代码可能会对您有所帮助。
BiConsumer biConsumer = (e, i) -> lmod.set(i, e*2);
lmod.forEachWithIndex(biConsumer);
另外,我相信将BiConsumer传递给方法时,将在内部使用它,在这种情况下,如果将参数设置为Object
,则需要进行casting
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.