Java8 LAMDBA Expression 001

Source: Internet
Author: User

In an already existing programming language, there are few new features that have a significant impact on existing ecosystems. A lambda expression is the existence of this meaning for the Java language. In simple terms, lambda expressions provide a convenient way to create an anonymous feature. Provides an easy way to create a simple method using an expression or a series of states. A lambda expression is an interface that contains an abstract method. LAMDBA expressions can be used to create simple anonymous functions that enable sorting and filtering of collection elements, as well as assigning variables and passing them to other objects. Wait a minute. The following examples will be combined to deepen your understanding of LAMDBA.

First of all, the installation configuration to use LAMDBA, you need to install jdk1.8 you can download to the official website as to the environment variable configuration reference my other blog or baidu|google.

(Since I'm a novice so use the IDE instead of Notepad), Eclipse version Kepler can search for eclipse Kepler download

Complete the above two steps to start Eclipse install Eclipse support for compiling JAVA8 plugins

After the installation is complete restart the LAMDBA tour ^_^

Let's start with a simple example.

New Project JAVA8 Package name Java.lamdba

Nonsense, less code.

001

public class Hellolambda {public interface hellotype{void Hello (String text);} public static void Main (string[] args) {Hellotype Hellolambda = (String text), {System.out.println ("Hello" + text);} ; Hellolambda.hello ("Lambda");}}

An LAMDBA expression is an anonymous block of code that encapsulates an expression or a series of States and returns a value. LAMDBA expressions are also similar to the concepts of closures in other languages. It accepts 0 or more parameters [similar to the following are used num[+ ...] Representation]. Each parameter can explicitly specify a type or it can not explicitly specify a type [if the type is not specified, Java automatically infers the type from the context].
As shown above, a LAMDBA expression contains a list of parameters, a character '---' that is not originally in Java, and a method body surrounded by curly braces.

(argument list), {body}
For a LAMDBA expression, a parameter list can contain 0+ parameters. If there are no arguments, then only a pair of empty parentheses is used. If you have only 1 parameters, you do not have to use parentheses. The type designation for each parameter is optional. If no type is specified, it is inferred from the context. If there is a 1+ line in the body, enclose it in curly braces.
Run the above program. You will see the desired output.

001 defines an interface that declares a method main writes the interface of the LAMDBA implementation according to (argument list), {body}; Pass a parameter declaration or do not declare the type (text) method in the body to print "Hello" + text, and then call the Hello and pass the value result to print Hello Lamdba.

On the Code 002

public class Actioncodeexample {interface Actioncode{int ReturnCode (String codestr);}  public static void Main (string[] args) {/** * A lambda expression can contain any statement this an ordinary Java * method Contains. However, the continue and break keywords is illegal * at the top level. */actioncode code = (codestr), {switch (CODESTR) {case "ACTIVE": Return 0;case "INACTIVE": Return 1;default:return-1 ;}}; System.out.println (Code.returncode ("ACTIVE"));}}

The 002 method returns an int value that receives a string that returns an int based on the result of a switch string processing, and this example LAMDBA encapsulates the functionality of the method body.


Write some custom interfaces and then use LAMDBA expressions to implement them. All LAMDBA expressions implement a functional interface [an interface with only an abstract method].

On the Code 003

public class Reversetypeexample {interface Reversetype {string reverse (string text);} public static void Main (string[] args) {Reversetype NewText = (testtext), {string tempstr = ""; for (string part:tes Ttext.split ("")) {tempstr = new StringBuilder (part). Reverse (). toString (); return tempstr;}; System.out.println (Newtext.reverse ("HELLO"));}}
The 003 functional interface contains the definition of an abstract method, string reverse (string text), main uses the LAMDBA expression to implement the Reversetype, and then prints the return value Olleh.


In addition to using the above method, we can also use the class under the Java.util.function package to implement the LAMDBA expression, 004 will use the Function<t,r> interface to complete the 31-like function. On code 004:

Import Java.util.function.function;public class Functionexample {public static void main (string[] args) {/** * accepts a s Tring argument and returns a string result */function<string, string> NewText = (testtext), {string tempstr = " "; for (String Part:testText.split (") ") {TempStr = new StringBuilder (part). Reverse (). toString (); return tempstr;}; System.out.println (Newtext.apply ("World"));}}

Notice that we have imported the package java.util.function.Function; the format is Function<arugent list,return result>,function is generic: The left is the type of parameter received, The return type is on the right. and called by apply (). 004 Print out the Dlrow.

Looking at these examples, you might think that if you need a function, write your own implementation. Java provides us with a lot of features


Using the function interface under the Java.util.function package can greatly reduce the code you are writing and improve the performance of your code.

In the next article we'll use the LAMDBA expression to tell the simple sort, this is it.

PS: This example is quoted from Josh Juneau Java 8 Recipes, 2nd Edition

Mission completed!

O (∩_∩) o~

Java8 LAMDBA Expression 001

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.