Java 8 Lambda, javalam.pdf

Source: Internet
Author: User

Java 8 Lambda, javalam.pdf

Some new projects within the company adopt Java 8 one after another, so that we have the motivation to study Java 8's legendary big killer. The first improvement mentioned in the Java 8 document is Lambda, starting with it. Many gods in the community have been studying for several years. If there is any inaccuracy, I hope to correct it.

Why Lambda?

To write Java code more concisely, to put it bluntly, it means that the length of the written code is shorter. So simple? Core: Yes, but it may bring some additional benefits. Theoretically, Lambda is not a necessity, and the code written is more elegant and concise. One of the most criticized problems of Java is the large amount of code (many of which are useless, but only the syntax requirements are required to do so). Lambda can slim down Java code. The Java community has been calling for Lambda for a long time. Finally, Java 8 is coming, and projects are gradually used. Sahua...

What is Lambda?

In Java, Lambda is short for Functional interfaces. It is essentially a function Interface..

  • What is a function interface?
    Function interfaces are special interfaces with only one abstract method.
  • What is an abstract method?
    Abstract methods are not implemented, that is, method parameters are not followed by braces

In this case, Lambda is an interface, which is at the same level as a class, rather than a method in a class. I'm surprised to hear that Lambda in Python is called an anonymous function, a function, or a class. I guess that Java is designed to weigh the results of forward compatibility and other factors.

Therefore, any Lambda in Java must have a corresponding function interface. The function interface is of the Lambda type, and Lambda is an instance of the function interface. The function interface must be declared in advance and should be written again when Lambda is in use. So annoying. Do I have to write function interfaces first when using Lambda? Yes, but this is taken into account during JDK design. Several common function interfaces are provided in advance under the java. util. function package, such as Predicate and Consumer.

Lambda syntax

Lambda consists of three parts: parameter,->, method body

  • Parameters
    (ParamType1 p1, ParamType2 p2 ,...)
    The parameter type can be saved and changed to (p1, p2 ,...)
    Only one parameter can be saved in parentheses, p1

  • Method body
    A return statement does not have to be written for an expression or statement block.
    The expression has a calculation result.
    Statement blocks are enclosed in braces (multiple statements can be written). When there is only one statement and the return value of the abstract method of the function interface is void, braces can be saved.

Lambda example
public class Lambda {    public static void main(String[] args) {        List<Material> materials = new ArrayList<Material>();        materials.add(new Material());        materials.add(new Material());        materials.add(new Material());        materials.add(new Material());        materials.add(new Material());        doSomthing(materials,                (Material m) -> System.out.println("Print " + m.getId()));    }    public static void doSomthing(List<Material> materials,            FunctionalInterfaceDemo demo) {        for (Material material : materials) {            demo.print(material);        }    }    static class Material {        public static int idStore = 1;        private int id;        public Material() {            id = idStore++;        }        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }    }    static interface FunctionalInterfaceDemo {        void print(Material material);    }}

FunctionalInterfaceDemo is a function interface.

Conclusion

The parameter type can be left empty. However, JDK willCompile timeSpeculate on the function interface corresponding to Lambda to know the abstract method information. In addition, note that Lambda will not generate a new scope (although with braces), which is the same as the scope where Lambda is located. Although Lambda is not a necessity, it is definitely not a luxury. It has been used in other languages for many years and has been used by students of Java 8.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.