简体   繁体   English

此表达式的目标类型必须是Java中的功能接口

[英]The target type of this expression must be a functional interface in java

lmod.forEachWithIndex( (e, i) -> lmod.set(i, e*2));

I am getting the error: 我收到错误消息:

The method forEachWithIndex(Object) in the type XList is not applicable for the arguments (( e, i) -> {}) The target type of this expression must be a functional interface XList类型的forEachWithIndex(Object)方法不适用于参数((e,i)-> {})此表达式的目标类型必须是功能接口

What does this mean? 这是什么意思? How can I fix it? 我该如何解决?

 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) {

   }

}

I add the function interface Consumer but I still do not know what to do next 我添加了功能接口Consumer,但我仍然不知道下一步该怎么做

You can bypass your error by below code but I will strongly recommend you to modify your forEachWithIndex parameter from Object to a BiConsumer 您可以通过以下代码绕过错误,但我强烈建议您将forEachWithIndex参数从Object修改为BiConsumer
like 喜欢

public void forEachWithIndex(BiConsumer biConsumer) {}

If you still want to strict to your implementation the here is the code may help you. 如果您仍然想严格执行,这里的代码可能会对您有所帮助。

BiConsumer biConsumer = (e, i) -> lmod.set(i, e*2);
lmod.forEachWithIndex(biConsumer);

Also, I believe when you pass the BiConsumer to a method you are going to use it inside, in that case, you need to do the casting if you put the parameter as Object . 另外,我相信将BiConsumer传递给方法时,将在内部使用它,在这种情况下,如果将参数设置为Object ,则需要进行casting

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sfl4j 和 java 8:这个表达式的目标类型必须是函数接口 - 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 获取错误::此表达式的目标类型必须是函数式接口 - Getting Error :: The target type of this expression must be a functional interface “此表达式的目标类型必须是功能接口”,即使它是 - "The target type of this expression must be a functional interface" even though it is 目标类型的表达式必须是一个功能接口 - 为什么需要它? - The target type of expression must be a functional interface – why is this needed? 映射数据集行得到“此表达式的目标类型必须是功能接口” - Mapping Dataset row getting “The target type of this expression must be a functional interface” 此表达式的目标类型必须是函数接口:Function vs Consumer - The target type of this expression must be a functional interface: Function vs Consumer 我收到错误消息:该表达式的目标类型必须是功能接口 - I get the error: The target type of this expression must be a functional interface 使用 Predicate 方法 - “此表达式的目标类型必须是功能接口” - Using Predicate method - "The target type of this expression must be a functional interface" 此表达式的目标类型应该是功能接口 - The target type of this expression should be a functional interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM