Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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_Lambda_Functional Programming - Fatal编程技术网

函数,它接受各种类型的参数并在函数Java中返回特定对象

函数,它接受各种类型的参数并在函数Java中返回特定对象,java,lambda,functional-programming,Java,Lambda,Functional Programming,假设我有一些lambda表达式,如下所示: Function<String, List<String>> flines = fileName -> { //Puts all lines from file into List and returns a list }; Function<List<String>, String> join = listOfLines -> {

假设我有一些lambda表达式,如下所示:

        Function<String, List<String>> flines = fileName -> {
    //Puts all lines from file into List and returns a list
        };

        Function<List<String>, String> join = listOfLines -> {
    //Concatenates all lines from list and returns them as String
        };

        Function<String, List<Integer>> collectInts = str -> {
    //Returns all Integer occurences from given String
        };
Function flines=fileName->{
//将文件中的所有行放入列表并返回一个列表
};
函数join=listOfLines->{
//连接列表中的所有行并将其作为字符串返回
};
函数collectInts=str->{
//返回给定字符串中出现的所有整数
};
我想创建一个函数,比如说
resultType convertBy(function…args)
所以我可以组合参数并在结果中返回:

List<String> lines = fileConv.convertBy(flines) //returns list of lines
String text = fileConv.convertBy(flines, join); //returns s String from concatenated Strings
List<Integer> ints = fileConv.convertBy(flines, join, collectInts); //returns list of Integers from string
Integer sumints = fileConv.convertBy(flines, join, collectInts, sum); ////returns list of Integers from string and sums it
List lines=fileConv.convertBy(flines)//返回行列表
String text=fileConv.convertBy(flines,join)//从连接的字符串返回s字符串
List ints=fileConv.convertBy(flines、join、collectInts)//返回字符串中的整数列表
整数sumints=fileConv.convertBy(flines、join、collectInts、sum)////返回字符串中的整数列表并对其求和
在Java中这样做有可能吗


编辑:在使用泛型时,我不能使用重载,您需要声明所涉及的类型变量。因为定义一个使用可变数量的泛型函数(带有varargs)来链接调用的方法需要可变数量的类型变量,所以这是不可能的

在编译时,不可能保证使用varargs给出的每个函数都使用类型,以便它们在链接调用时兼容。 您可以这样做,但不能以类型安全的方式进行。函数的输入/输出类型的任何不匹配都将导致运行时出现
ClassCastException

private static <T, U> U convertBy(T arg, Function... functions) {
  Object result = arg;
  for (Function f : functions) {
    result = f.apply(result);
  }
  return (U) result;
}

@Test
public void test() {
  Function<String, Integer> f1 = s -> s.length();
  Function<Integer, Double> f2 = i -> i*2.0;
  Double d = convertBy("test", f1, f2);
  assertThat(d).isEqualTo(8.0);
}
private static U convertBy(T参数,函数…函数){
对象结果=arg;
for(函数f:函数){
结果=f。应用(结果);
}
返回(U)结果;
}
@试验
公开无效测试(){
函数f1=s->s.length();
功能f2=i->i*2.0;
双d=转换比(“测试”,f1,f2);
资产(d)isEqualTo(8.0);
}
但是,您可以手动定义通过重载链接的方法的变体:

private static <T, U> U convertBy(T arg, Function<T, U> func1) {
  return func1.apply(arg);
}

private static <T, U, V> V convertBy(T arg, Function<T, U> func1, Function<U, V> func2) {
  return func2.apply(func1.apply(arg));
}

private static <T, U, V, X> X convertBy(T arg, Function<T, U> func1, Function<U, V> func2, Function<V, X> func3) {
  return func3.apply(func2.apply(func1.apply(arg)));
}
private static U convertBy(T参数,函数func1){
返回函数1.应用(arg);
}
专用静态V转换器(T参数,函数func1,函数func2){
返回func2.apply(func1.apply(arg));
}
专用静态X convertBy(T参数,函数func1,函数func2,函数func3){
返回func3.apply(func2.apply(func1.apply(arg));
}

为什么不简单地使用
flines.和(join).和(collections).和(sum)
?您以错误的方式处理问题。您希望使用命令式方法组合函数,而您应该按照@JBNizet的建议,使您的方法只接收一个函数,并使用函数组合将所有函数转换为您的方法可以处理的一个函数。