简体   繁体   English

将 Java 通用 Lambda 参数传递给方法本地调用

[英]Passing Java Generic Lambda Parameter Into Method Local Invocation

Here, I am trying to pass lambda argument into local invocation of a method that accepts lambda argument:在这里,我试图将 lambda 参数传递到接受 lambda 参数的方法的本地调用中:

package Desktop;

import java.util.function.*;

public class MyClass {

    public static <T, U> U myMethod(Function<T, U> myParam) {
        return otherMethod(myParam);
    }

    public static Long otherMethod(Function<String, Long> otherParam) {
        return otherParam.accept("900");
    }

    public static void main(String[] args) {
        System.out.println(myMethod(Long::valueOf));
    }
}

I browsed for some help on sites and it shows that we can pass lambda (with generics) from one parameter into another:我在网站上浏览了一些帮助,它表明我们可以将 lambda(带有泛型)从一个参数传递到另一个参数:

public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
    return Arrays.stream(a).map(mapperFunction).collect(Collectors.toList());
}

/*
    Signature for map() is like this:
    <R> Stream<R> map(Function<? super T, ? extends R> mapper)
*/

Now how to properly pass a lambda with generics from parameter method into another?现在如何正确地将带有泛型的 lambda 从参数方法传递到另一个方法?

One problem is that there are two signatures of valueOf .一个问题是valueOf有两个签名。 One takes a primitive long, the other takes a string.一个需要一个原始 long,另一个需要一个字符串。

There is no way for Java to know which one you mean. Java 无法知道您指的是哪一个。

You can manually specify by using a type witness:您可以使用类型见证手动指定:

System.out.println(MyClass.<String, Long>myMethod(Long::valueOf));

or, if you prefer, assign the method reference to a variable so that additional type information can be inferred from the variable.或者,如果您愿意,可以将方法引用分配给变量,以便可以从变量中推断出其他类型信息。

Function<String, Long> func = FakeLong::valueOf;
System.out.println(MyClass.myMethod(func));

Another problem is here:另一个问题在这里:

return otherMethod(myParam);

Fact is, otherMethod specifically requires a function that takes a String and returns a long, while myParam can be any function at all.事实是, otherMethod特别需要一个函数,它接受一个 String 并返回一个 long,而myParam可以是任何函数。 There is a fundamental incompatibility here.这里有一个根本的不兼容。 Your design is wrong.你的设计是错误的。

The problem with your code in this line public static Long otherMethod(Function<String, Long> otherParam)此行中的代码问题public static Long otherMethod(Function<String, Long> otherParam)

You are calling non generic method form generic method, because of this eventually you have to cast your parameter which is not good thing to avoid exceptions while running your code您正在从泛型方法中调用非泛型方法,因此最终您必须强制转换参数,这对于在运行代码时避免异常来说并不是一件好事

try below code试试下面的代码

public static <T extends String, U extends Long> U myMethod(Function<T, U> myParam, T value) {
        return otherMethod(myParam, value);
    }

    public static <T, U> U otherMethod(Function<T, U> otherParam,T value) {
        return otherParam.apply(value);
    }

    public static void main(String[] args) {
        System.out.println(myMethod(Long::valueOf, "900"));
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM