Java Lambda和泛型方法

Java Lambda和泛型方法,java,generics,lambda,Java,Generics,Lambda,我正在学习Lambda,我遇到了一些我无法解决的问题 最初我的代码是: package Lambdas; @FunctionalInterface interface NumericFunc2 { <T extends Number> T func(T n); } class Lbd_488_NumericFunc_SelfTest { public static void main(String args[]) { // This block l

我正在学习Lambda,我遇到了一些我无法解决的问题

最初我的代码是:

package Lambdas;

@FunctionalInterface
interface NumericFunc2 {
    <T extends Number> T func(T n);
}

class Lbd_488_NumericFunc_SelfTest {
    public static void main(String args[]) {
        // This block lambda returns the smallest positive factor of a value.
        NumericFunc2 smallestF = (n) -> {
            double result = 1;
            // Get absolute value of n.
            n = n < 0 ? -n : n;
            for (int i = 2; i <= n / i; i++)
                if ((n % i) == 0) {
                    result = i;
                    break;
                }
            return result;
        };
        System.out.println("Smallest factor of 12 is " + smallestF.func(12));
        System.out.println("Smallest factor of 11 is " + smallestF.func(11));
    }
}
但是,如果我将类从非泛型更改为泛型,一切都会正常运行,如下代码所示:

package Lambdas;

@FunctionalInterface
interface NumericFunc2<T extends Number>  {
    T func(T n);
}

class Lbd_488_NumericFunc_SelfTest {
    public static void main(String args[]) {
        // This block lambda returns the smallest positive factor of a value.
        NumericFunc2<Double> smallestF = (n) -> {
            double result = 1;
            // Get absolute value of n.
            n = n < 0 ? -n : n;
            for (int i = 2; i <= n / i; i++)
                if ((n % i) == 0) {
                    result = i;
                    break;
                }
            return result;
        };
        System.out.println("Smallest factor of 12 is " + smallestF.func(12));
        System.out.println("Smallest factor of 11 is " + smallestF.func(11));
    }
}
包Lambdas;
@功能接口
接口数值函数2{
T函数(tn);
}
类Lbd_488_NumericFunc_自检{
公共静态void main(字符串参数[]){
//此块lambda返回值的最小正因子。
NumericFunc2 smallestF=(n)->{
双结果=1;
//得到n的绝对值。
n=n<0?-n:n;
对于(int i=2;i来说,答案是不能对
Number
对象执行此类数值操作。这是不可能的;这与lambdas无关,与
Number
抽象类有关,只是不提供这些特性。

在不知道一个数是什么类型的情况下,甚至没有任何方法可以得到它的绝对值。你看不出它是正的还是负的,你不能否定它,你不能加、减、乘它。你唯一能做的就是把它转换成另一个基元类型,但是您无法将其转换回去,也无法对其进行数学运算。

根据,泛型和lambdas不能一起工作。请使用一些代码示例讨论它

为了使它正常工作,您必须返回到。即,您必须围绕您的函数编写完整的
newnumberfunc(){…}
mambo jambo

我必须对方法体进行一些其他更改,以使其能够编译:

  • result
    是装箱的
    Double
    ,因此我可以将其转换为
    T
  • 在方法内部,我使用了n的doubleValue(),因为基元运算符不适用于
    t extends Number
把它放在一起:

class Lbd_488_NumericFunc_SelfTest {
    public static void main(String args[]) {
        // This block lambda returns the smallest positive factor of a value.
        NumericFunc2 smallestF = new NumericFunc2() {
            public <T extends Number> T func(T n) {
                Double result = 1d;
                // Get absolute value of n.
                double nAbs = n.doubleValue() < 0 ? -n.doubleValue() : n.doubleValue();
                for (int i = 2; i <= nAbs / i; i++)
                    if ((nAbs % i) == 0) {
                        result = (double) i;
                        break;
                    }
                return (T) result;
            }
        };
        System.out.println("Smallest factor of 12 is " + smallestF.func(12));
        System.out.println("Smallest factor of 11 is " + smallestF.func(11));
    }
}
类Lbd\u 488\u NumericFunc\u自检{
公共静态void main(字符串参数[]){
//此块lambda返回值的最小正因子。
NumericFunc2 smallestF=新的NumericFunc2(){
公共T函数(T n){
双结果=1d;
//得到n的绝对值。
double-nAbs=n.doubleValue()<0?-n.doubleValue():n.doubleValue();

对于(int i=2;我知道,我理解你的意思。但是,如果我从“数字”改为“双精度”,我仍然得到相同的错误。此外,如中所述,如果以下所有条件均为真,则可以看到lambda表达式与函数类型一致:…函数类型没有类型参数,因此不能将lambda分配给函数签名中包含类型变量的函数接口(这与拥有通用功能接口不同)您的接口也不是您认为它的意思。当您编写
t func(tn);
时,您所说的是接口的每个实现(包括lambda)必须有一个
func
的实现,该实现适用于任何
T extends Number
。即使您没有使用lambdas,您的界面也不允许您只对
Number
的一个子类型有一个实现。我如何以一种有效的方式编写此代码?保持方法泛型和类非泛型?@Jack's link说明您不能使用lambda执行此操作。如果您处理了
Number
无法执行此操作的问题,您可以使用匿名类执行此操作。提示-不要在lambda中的
{}
内部编写嘈杂的代码(超过一行),请改用
方法()
class Lbd_488_NumericFunc_SelfTest {
    public static void main(String args[]) {
        // This block lambda returns the smallest positive factor of a value.
        NumericFunc2 smallestF = new NumericFunc2() {
            public <T extends Number> T func(T n) {
                Double result = 1d;
                // Get absolute value of n.
                double nAbs = n.doubleValue() < 0 ? -n.doubleValue() : n.doubleValue();
                for (int i = 2; i <= nAbs / i; i++)
                    if ((nAbs % i) == 0) {
                        result = (double) i;
                        break;
                    }
                return (T) result;
            }
        };
        System.out.println("Smallest factor of 12 is " + smallestF.func(12));
        System.out.println("Smallest factor of 11 is " + smallestF.func(11));
    }
}