Do lambda expressions have any use other than saving lines of code?

后端 未结 9 1473
我寻月下人不归
我寻月下人不归 2020-11-29 18:23

Do lambda expressions have any use other than saving lines of code?

Are there any special features provided by lambdas which solved problems which weren\'t easy to s

相关标签:
9条回答
  • 2020-11-29 18:57

    There are many benefits of using lambdas instead of inner class following as below:

    • Make the code more compactly and expressive without introducing more language syntax semantics. you already gave an example in your question.

    • By using lambdas you are happy to programming with functional-style operations on streams of elements, such as map-reduce transformations on collections. see java.util.function & java.util.stream packages documentation.

    • There is no physical classes file generated for lambdas by compiler. Thus, it makes your delivered applications smaller. How Memory assigns to lambda?

    • The compiler will optimize lambda creation if the lambda doesn't access variables out of its scope, which means the lambda instance only create once by the JVM. for more details you can see @Holger's answer of the question Is method reference caching a good idea in Java 8? .

    • Lambdas can implements multi marker interfaces besides the functional interface, but the anonymous inner classes can't implements more interfaces, for example:

      //                 v--- create the lambda locally.
      Consumer<Integer> action = (Consumer<Integer> & Serializable) it -> {/*TODO*/};
      
    0 讨论(0)
  • 2020-11-29 18:59

    Saving lines of code can be viewed as a new feature, if it enables you to write a substantial chunk of logic in a shorter and clearer manner, which takes less time for others to read and understand.

    Without lambda expressions (and/or method references) Stream pipelines would have been much less readable.

    Think, for example, how the following Stream pipeline would have looked like if you replaced each lambda expression with an anonymous class instance.

    List<String> names =
        people.stream()
              .filter(p -> p.getAge() > 21)
              .map(p -> p.getName())
              .sorted((n1,n2) -> n1.compareToIgnoreCase(n2))
              .collect(Collectors.toList());
    

    It would be:

    List<String> names =
        people.stream()
              .filter(new Predicate<Person>() {
                  @Override
                  public boolean test(Person p) {
                      return p.getAge() > 21;
                  }
              })
              .map(new Function<Person,String>() {
                  @Override
                  public String apply(Person p) {
                      return p.getName();
                  }
              })
              .sorted(new Comparator<String>() {
                  @Override
                  public int compare(String n1, String n2) {
                      return n1.compareToIgnoreCase(n2);
                  }
              })
              .collect(Collectors.toList());
    

    This is much harder to write than the version with lambda expressions, and it's much more error prone. It's also harder to understand.

    And this is a relatively short pipeline.

    To make this readable without lambda expressions and method references, you would have had to define variables that hold the various functional interface instances being used here, which would have split the logic of the pipeline, making it harder to understand.

    0 讨论(0)
  • 2020-11-29 19:01

    Lambdas are just syntactic sugar for anonymous classes.

    Before lambdas, anonymous classes can be used to achieve the same thing. Every lambda expression can be converted to an anonymous class.

    If you are using IntelliJ IDEA, it can do the conversion for you:

    • Put the cursor in the lambda
    • Press alt/option + enter

    0 讨论(0)
提交回复
热议问题