将lambda表达式转换回内部类?

时间:2016-07-28 06:51:22

标签: java lambda java-8

我有以下lambda表达式:

Function<Integer, Function<Integer, Function<BinaryOperator<Integer>, Integer>>> someComputation = i1 -> i2 -> f -> f.apply(i1, i2);

Java 8 lambda语法对我来说仍然有点陌生,所以我试图将它转换回内部类但似乎我无法转换它会抛出编译错误或者我错过了一些东西。 我认为这是:

    Function<Integer, Function<Integer, Function<BinaryOperator<Integer>, Integer>>> someComputation2 = new Function<Integer, Function<Integer, Function<BinaryOperator<Integer>, Integer>>>() {
        @Override
        public Function<Integer, Function<BinaryOperator<Integer>, Integer>> apply(Integer integer) {

            Function<Integer, Function<BinaryOperator<Integer>, Integer>> f = new Function<Integer, Function<BinaryOperator<Integer>, Integer>>() {
                @Override
                public Function<BinaryOperator<Integer>, Integer> apply(Integer integer) {


                    return new Function<BinaryOperator<Integer>, Integer>() {
                        @Override
                        public Integer apply(BinaryOperator<Integer> integerBinaryOperator) {
                            return integerBinaryOperator.apply(f,""" WHAT IS IN HERE ???""");
                        }
                    };



                }
            };

            return f;
        }
    };

2 个答案:

答案 0 :(得分:3)

外部匿名类实例的方法中有两个Integer变量。这些是应该传递给最内部方法调用的变量:

Function<Integer, Function<Integer, Function<BinaryOperator<Integer>, Integer>>> someComputation2 = new Function<Integer, Function<Integer, Function<BinaryOperator<Integer>, Integer>>>() {
    @Override
    public Function<Integer, Function<BinaryOperator<Integer>, Integer>> apply(Integer i1) {
        Function<Integer, Function<BinaryOperator<Integer>, Integer>> f = new Function<Integer, Function<BinaryOperator<Integer>, Integer>>() {
            @Override
            public Function<BinaryOperator<Integer>, Integer> apply(Integer i2) {
                return new Function<BinaryOperator<Integer>, Integer>() {
                    @Override
                    public Integer apply(BinaryOperator<Integer> integerBinaryOperator) {
                        return integerBinaryOperator.apply(i1, i2);
                    }
                };
            }
        };
        return f;
    }
};

答案 1 :(得分:1)

如果您使用的是Eclipse或其他等效的Java IDE,则可以生成一些框架代码,然后连接您想要的实现。像你期望的那样是

Function<Integer, Function<Integer, Function<BinaryOperator<Integer>, Integer>>> someComputation = new Function<Integer, Function<Integer,Function<BinaryOperator<Integer>,Integer>>>() {

        @Override
        public Function<Integer, Function<BinaryOperator<Integer>, Integer>> apply(Integer t1) {
            return new Function<Integer, Function<BinaryOperator<Integer>,Integer>>() {

                @Override
                public Function<BinaryOperator<Integer>, Integer> apply(Integer t2) {
                    return new Function<BinaryOperator<Integer>, Integer>() {

                        @Override
                        public Integer apply(BinaryOperator<Integer> t3) {
                            return t3.apply(t1, t2);
                        }
                    };
                }
            };
        }
    };