Java8 Lambda Expressions Getting Started

Source: Internet
Author: User
Tags stream api

The essence of a lambda expression is an anonymous function. c#3.0 introduced lambda expressions, and Java8 is not outdone. Java8 was released for a long time, and today the JDK was installed to experience the lambda expression in Java8.

First look at an example that does not apply to lambda expressions.
For example, we want to sort a set of strings.

public  class  Hello { Span class= "Hljs-keyword" >public static  void  main  (string[] args) {list<string> names = arrays.aslist ( "Tan" ,  "Zhen" , ); Collections.sort (names, new  comparator<string> () {@Override public  int  compare  (String A, string b) {return  A.compareto (b); } }); for  (String name:names) {System. out . println (name); } }}

Operation Result:

Tanyuzhen

The same example looks at the code that uses a lambda expression:

publicclass Hello {    publicstaticvoidmain(String[] args) {        List<String> names = Arrays.asList("Tan""Zhen""Yu");        Collections.sort(names, (String a, String b) -> a.compareTo(b));        Stream<String> stream = names.stream();        stream.forEach(System.out::println);    }}

is not much more concise.

The following is a detailed description of the lambda expression in the next Java8.
(String A, string b)-A.compareto (b) is a lambda expression. In the preceding brackets is the function's argument list,-> symbol followed by the function body. So the lambda notation is to use parentheses to list the function arguments, and then to point to the function body with the ampersand, which is generally enclosed in curly braces {}.
The above function body A.compareto (b) is actually a shorthand. The complete wording should be

(String A, string B), {    Returen a.compareto (b);}

Because the function body has only one sentence, you can generally omit the curly brace and the return keyword.

Each lambda expression in Java corresponds to a type, usually an interface type, annotated with @functionalinterface. This "functional interface" refers to an interface that contains only an abstract method, and each lambda expression of that type is matched to this abstract method. That is, each lambda expression corresponds to that abstract method in the functional interface.
In addition, Java 8 allows us to add a non-abstract method implementation to the interface, only need to use the default keyword, which is called the extension method. So there can only be one abstract method in a functional interface, and the other can only be extension methods. The following is the comparator interface in Java8, compare is an abstract method, and there are some extension methods.

@FunctionalInterfacepublic interface Comparator {     int compare (t O1, T O2);    ......}


In the example code above, Stream.foreach (System.out::p rintln) is used to output each string using the stream API, which is explained in more detail below.
Java 8 introduces a new stream API. This stream differs from the I/O stream in that it is more like a collection class with iterable, but behaves differently from the collection class. The Stream API is introduced to compensate for the flaws in Java functional programming. For many languages that support functional programming, map () and reduce () are basically built into the language's standard libraries (such as those in Python and JavaScript).
There are many ways to create a stream, and the simplest way to do this is to turn a collection into a stream, like the above stream<string> stream = Names.stream ().
The new stream () method of the collection class is used to turn a collection into a stream, and then to transform the stream by using filter (), map (), and so on. Stream also has a foreach () to complete the iteration of each element.

This is the signature of the Foreach method void foreach (consumer<? Super t> Action). consumer<? Super T> is also a functional interface, so we should pass in a function.
Java 8 allows you to use the:: keyword to pass a method or constructor reference, like System.out above::p rintln is a reference to the PRINTLN function. If the reference constructor can use the object:: New form.

The function of the Java8 in a personal sense is similar to the proxy type (delegate) in C #, or more like a function.

Java8 Lambda Expressions Getting Started

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.