Lambda expressions for new features of JAVA8

Source: Internet
Author: User
Tags pear

Lambda expression

Simplifying the process of instantiating a functional interface (functional interface)

The benefits of lambda expressions

Simplifying the implementation of code

The syntax of a lambda expression

    • A LAMBDA expression can have 0 or more parameters
    • The type of the parameter can be either explicitly declared or inferred from the context. For example: (int a) the same as the (a) effect
    • All parameters need to be enclosed in parentheses, and the parameters are separated by commas. For example: (a, b) or (int a, int b) or(String a, int b, float c)
    • An empty parenthesis indicates that the parameter set is empty. For example:() -> 42
    • When there is only one argument, and its type is inferred, parentheses () can be omitted. For example:a -> return a*a
    • The body of a LAMBDA expression can contain 0 or more statements
    • If the body of a LAMBDA expression has only one statement, the curly brace {} can be omitted. The return type of an anonymous function is consistent with the body expression
    • If the body of a LAMBDA expression contains more than one statement, the expression must be enclosed in curly braces {} (forming a code block). The return type of the anonymous function is the same as the return type of the code block, or null if no return

Use of lambda expressions

1. Examples of lambda expressions for functional interfaces

Traditional implementations  Public void Runthread () {    new Thread (new  Runnable () {        publicvoid  Run () {            System.out.println ("run!" );        }    }). Start ();}
lambda expression Implementation  Public void Runthreaduselambda () {    new Thread ((), {        System.out.println("run!") );    }). Start ();}

2. Method Reference and Construction reference (method reference is another way to implement a functional interface)

= Arrays.aslist (new string[] {"Apple", "orange", "pear", System.out.println (v));
= Arrays.aslist (new string[] {"Apple", "orange", "Pear"}); Input.foreach (System.out::p rintln);

3.jdk8 for collection operations (introduction of Java.util.stream.Stream, default method + function interface as parameter +LAMBDA expression)

Traditional Implementation list<Integer> list = arrays.aslist (1,2,3,4,5,6,7);  for (Integer n:list) {    int x = n * N;    SYSTEM.OUT.PRINTLN (x);}
Stream implements list<Integer> list = arrays.aslist (1,2,3,4,5,6,7-x*x). ForEach (System.out:: PRINTLN);

Reference links

Http://blog.oneapm.com/apm-tech/226.html

Http://baike.baidu.com/item/Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F

Http://www.infoq.com/cn/articles/Java-se-8-lambda

Https://dzone.com/articles/why-we-need-Lambda-expressions

Lambda expressions for new features of JAVA8

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.