Lambdas in the classical Operation enum example

前端 未结 4 2154
长情又很酷
长情又很酷 2020-12-01 05:16

As many of you may know, there is a classical example of the Operation enum (using Java 8 standard interface now though), that is the following:



        
4条回答
  •  长情又很酷
    2020-12-01 05:42

    It depends how you define better.

    In your case and in my opinion, the lambdas are a pure win. You can even reuse some existing JDK functions, e.g.:

    enum Operation implements DoubleBinaryOperator {
        PLUS    ("+", Double::sum),
        ...
    }
    

    This is short and readable. I don't think anything reasonable can be said about performance w/o benchmarking your code.

    Lambdas are implemented with invokeDynamic to dynamically link call site to actual code to execute; no anonymous, inner classes.

提交回复
热议问题