lambda expression vs static method

I had a question about reusability of lambda expression without code duplication. For example if I have a helper method I can easily code it as a static method and can refer to it from other classes without code duplication. How would this work in lambda expression ? Example: I have the following static method written

public class MyUtil {

    public static int doubleMe(int x) {
        return x * 2;
    }
}

I can reuse the same method without code duplication in multiple places across the project

public class A {

    public void someOtherCalculation() {
        MyUtil.doubleMe(5);
    }
}

public class B {

    public void myCalculation() {
        MyUtil.doubleMe(3);
    }
}

How would it work when it comes to a lambda function, write the function once and use the same at multiple class.

Function<Integer, Integer> doubleFunction = x -> x * 2;

In my example, where would I write the above lambda function and how would I reuse the same in class A and B ?


Where would I write the above lambda function

Since your function does not reference any fields, it is appropriate to put it in a static final field:

class Utility {
    public static final Function<Integer,Integer> doubleFunction = x -> x * 2;
}

how would I reuse the same in class A and B?

You would refer to it as Utility.doubleFunction , and pass it in the context where it is required:

callMethodWithLambda(Utility.doubleFunction);

Note that method references let you define a function, and use it as if it were lambda:

class Utility {
    public static Integer doubleFunction(Integer x) {
        return x*2;
    }
}
...
callMethodWithLambda(Utility::doubleFunction);

This approach is very flexible, because it lets you reuse the same code in multiple contexts as you find appropriate.


Really, anonymous functions are for cases where code reuse isn't necessary.

Dumb example, but say you're using map to add two to every number in a list. If this is a common action that you may need all over the place, a static function that adds two to a number makes more sense than writing the same lambda everywhere.

If, however you have a single function that adds two to a list, it makes more sense to define the "add two" function locally as a lambda so you dont plug up your class with code that isn't needed anywhere else.

When writing Clojure, which makes extensive use of higher-order functions, it's pretty common for me to create local anonymous functions that tidy up the code in the "full" function that I'm writing. The vast majority of these anonymous functions would be non-sensical in the "global" scope (or class-scope); especially since they usually have closures over local variables, so they couldn't be global anyways.


With lambda expressions, you don't need to worry about reusability (in fact, most of the lambdas are not being re-used at all). If you want a Function pointer to point to this method the you can declare one like below:

Function<Integer, Integer> doubleFunction = MyUtil::doubleMe;

And pass it to any method or stream to apply/map, eg:

public static void consume(Function<Integer, Integer> consumer, int value){
    System.out.println(consumer.apply(value));
}

public static void main(String[] args) throws Exception{
    Function<Integer, Integer> doubleFunction = MyUtil::doubleMe;
    consume(doubleFunction, 5);
}
链接地址: http://www.djcxy.com/p/51556.html

上一篇: 模板静态变量

下一篇: lambda表达式与静态方法