In-depth lambda expression-0 basic one-minute primer

Source: Internet
Author: User

Lambda from the point of use, the first sense of the straightforward understanding is that there is a lot less unnecessary anonymous callback class, such as:

     Public Static void Main (string[] args) {        new  platformquery (). Createdemo ();         () ,  {System.out.println (Jsonutils.tojson (query));};         New Thread (c);        Thread.Start ();    }

In this code, you will print out:

{"Lists": [{"NodeId": 100001, "nodeName": "Agency 100001", "CompanyID": "" "," CreateDate ":" 20160101 "},{" nodeId ": 100001, "NodeName": "Agency 100001", "CompanyID": "$", "CreateDate": "20160101"}]}

Before Java 8, if we were to start a new thread, we would need to create a new class or an anonymous implementation class, implement the Run () method in the Runnable interface, and then pass a thread as a parameter to complete it.

With lambda, you don't need to create a new class that implements the Runaable interface, which you can do directly in the context of the main thread. where run () is implemented is {System.out.println (Jsonutils.tojson (query)) above,};, () is the signature of the Run method.

From the code's intuitive perspective, it does simplify a lot.

For example, for list ordering, in Java 8, we can use List.sort directly (Comparator) as follows:

        Query.getlists (). Sort (new comparator<platform>() {            @Override            public  int  Compare (Platform A, Platform b) {                return  integer.compare (A.getcompanyid (). length (), B.getcompanyid (). Length ());            }                    );        

With lambda expressions, we can use the following two types of notation:

        return Integer.compare (A.getcompanyid (). Length (), B.getcompanyid (). Length ());};        Query.getlists (). sort (COM);         return Integer.compare (A.getcompanyid (). Length (), B.getcompanyid (). Length ()); }); --This is an example of using anonymous lambda expressions directly

Further, Lambda is able to infer the context automatically, more than the generic compile-time erase (erase), as follows:

        return Integer.compare (A.getcompanyid (). Length (), B.getcompanyid (). Length ());};                Query.getlists (). sort (COM1);                 return Integer.compare (A.getcompanyid (). Length (), B.getcompanyid (). Length ()); });

As can be seen from the above, this is more like the common use of bound events in jquery, and everything is resolved by passing an anonymous function, for Java-based GUI programming or for applications that rely on a wide range of callback functions such as spring JDBC. You can make your code remove a lot of unnecessary skeleton code.

In-depth lambda expression-0 basic one-minute primer

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.