Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 变量(函数类型)参数计数和变量返回类型_Java_Generics_Functional Interface - Fatal编程技术网

Java 变量(函数类型)参数计数和变量返回类型

Java 变量(函数类型)参数计数和变量返回类型,java,generics,functional-interface,Java,Generics,Functional Interface,我有以下问题-我必须编写一个方法,它将接受可变数量的函数参数,并在此基础上返回所需的类型。问题是,参数中的函数按顺序运行: public class Main { public static void main(String[] args) throws Exception { //Lambda expressions which will be taken as arguments in method. Function<String,List<Stri

我有以下问题-我必须编写一个方法,它将接受可变数量的函数参数,并在此基础上返回所需的类型。问题是,参数中的函数按顺序运行:

public class Main {
    public static void main(String[] args) throws Exception {

    //Lambda expressions which will be taken as arguments in method.

    Function<String,List<String>> flines = e->{
    };
    Function<List<String>,String> join = e ->{
    };
    Function<String,List<Integer>> collectInts = e->{
    };
    Function<List<Integer>,Integer> sum = e->{
    };

    // end of lambdas

    String fname = System.getProperty("C:/LamComFile.txt"); 
    InputConverter<String> fileConv = new InputConverter<>(fname);
    List<String> lines = fileConv.convertBy(flines);
    String text = fileConv.convertBy(flines, join);
    List<Integer> ints = fileConv.convertBy(flines, join, collectInts);
    Integer sumints = fileConv.convertBy(flines, join, collectInts, sum);

    System.out.println(lines);
    System.out.println(text);
    System.out.println(ints);
    System.out.println(sumints);

    List<String> arglist = Arrays.asList(args);
    InputConverter<List<String>> slistConv = new InputConverter<>(arglist);  
    sumints = slistConv.convertBy(join, collectInts, sum);
    System.out.println(sumints);

    }
}

您无法在Java的类型系统中表达这一点。接受对其他事物的需要。例如,您可以只编写
f1.和第(f2).和第(f3).和第(f4).

在Java的类型系统中无法表达这一点。接受对其他事物的需要。例如,您可以只编写
f1.和第(f2).和第(f3).和第(f4).

是的,问题是我不能更改主文件,除了lambdas部分。现在每个示例有4个方法,看起来(也很糟糕)如下:(public O convertBy(Function flines,Function join…)是的,除了lambdas部分之外,我不能更改主文件。现在每个示例有4个方法,看起来(也很糟糕)如下:(public O convertBy(函数flines,函数join…)。你们都在同一个类中吗?那个赋值毫无意义。无法使用varargs来表示连续类型必须匹配。可以执行
convertBy(函数…funcs)
但是你失去了所有的类型安全性。我同意这是一个非常奇怪的任务。@Holger:看来我不是第一个参加这个任务的人(你的命中率有1,2次是正确的)-这是大学的半年,另一个明天可能会收到这个荒谬的作业。我知道,你知道,几乎每个人都知道。而且,你不能超负荷使用方法的部分是几天前添加的,一开始它不存在。你们都在同一个班吗?那个作业是胡说八道。没有way使用varargs表示连续类型必须匹配。您可以执行
convertBy(Function…funcs)
,但随后您将失去所有类型安全性。我同意这是一个非常奇怪的赋值。@霍尔格:似乎我不是该赋值的第一个(您的命中率中有1,2个是正确的)-这是大学的半年,另一个明天可能会收到这个荒谬的作业。我知道,你知道,几乎每个人都知道。而且,你不能让方法过载的部分是在几天前添加的,一开始它并不存在。
  public class InputConverter<T> {
      private T obj;

      public InputConverter(T obj) {
           this.obj = obj;
      }

      public <what goes here?> and_here convertBy(Function<In,Out>... funcs){

      }
  }
if(funcs.length()==1){
    what? = funcs[0].apply(this.obj);
    return what?
}