Java lambda expression

Source: Internet
Author: User
Tags readable

One of the big highlights of Java 8 is the introduction of lambda expressions, which are more concise with the code they are designed for. When a developer writes a lambda expression, it is also compiled into a functional interface. The following example uses lambda syntax instead of an anonymous inner class, and the code is not only concise, but also readable. There is no old way to use lambda:
1
12345 button.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent ae){System.out.println("Actiondetected");}});
Using lambda:
123 button.addActionListener(()->{ System.out.println("Actiondetected");});
Let's look at a more obvious example. Old ways to not use lambda:
123456 Runnable runnable1=newRunnable(){@Overridepublic voidrun(){System.out.println("RunningwithoutLambda");}};
Using lambda:
123 Runnable runnable2=()->{System.out.println("RunningfromLambda");};
As you can see, the use of lambda expressions not only makes the code simple, but also readable, and most importantly, the amount of code that is reduced a lot. However, in some ways, these functions are already widely used in these JVM languages such as Scala.

Java lambda expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.