idea and the IDE support Java 8 features, including lambda expressions, repeatable annotations, compact profiles, and other features.Syntax for lambda expressionsBasic syntax:(parameters)-> expressionOr(parameters)->{statements;}
The following is a simple example of a Java
First lambda expression
Before the lambda appears, if we need to write a multithreaded may require the following way:
Runnable Runnable = new Runnable () {
@Override public
void Run () {
System.out.println ("Hello Runnable");
}
};
...
Thread.Start ();
The above example would be a lot simpler if you were to use a
Java core technology-New Features of Java 8-Lambda expressions1. General description
Java 8 new featuresFunctional InterfaceLambda expressions (closures)2 Java 8 new features
Oracle released the official Java 8 version in March 20
Introduction to Lambda
Lambda expressions are an important new feature in Java SE 8. Lambda expressions allow you to use expressions instead of functional interfaces. A lambda expression, like a method, provides a normal list of arguments and a body that uses those argument
Why use lambda expressionslet's look at a few examples:
The first example is to perform a task in a separate thread, as we usually do:
Class Worker implements Runnable {public
void run () {for
(int i = 0; i
The second example, the custom string comparison method (through the string length), generally does this:
Class Lengthcomparator
, Java does not have an advanced type of interface like Scala. If you want to declare a lambda expression, you must specify the target type. In fact, Java has to be backwards compatible, which is understandable, and Java is doing well for now. For example, Thread.stop () was
expressionBasic syntax:(parameters), expressionOr(parameters)->{statements;}Here is a simple example of a Java lambda expression:1. No parameters are required and the return value is 5 ()-5//2. Receives a parameter (numeric type) that returns twice times its value x-2 * x//3. Accept 2 parameters (numbers) and return their difference (x, y), x–y//4. Receives 2 in
this way, although new requirements are met, this practice obviously violates the DRY principle. We need to redesign them to enhance reusability. In the code for calculating Bond assets, Lambda expressions play two roles:
How to traverse
How to calculateWhen using object-oriented design, we will consider using the Strategy Pattern to separate the above two responsibilities. But here we use Lambda
references cannot exist independently and are always converted to instances of a functional interfaceYou can use this and the super parameter in a method referenceThis::equals equivalent to X->this,equals (x)3.5 constructor ReferenceA constructor reference is similar to a method reference, except that the method name is new. For example, Person::new is a reference to the person constructor. Which constructor is called depends on the context.You can e
: 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 inter
original type, entry 29 tells you the preference generic type, and entry 30 tells you to favor the generic method. This recommendation is important when using lambda expressions, because the compiler obtains most of the type information that allows it to be inferred from the generic type. If you do not provide this information, the compiler will not be able to type inference, you must manually specify the type in Lambdas, which will greatly increase
Lambda with receiver* A method in the body of a lambda function that can invoke a different object without any additional qualifiers, which is not found in Java.* Such lambda is called "Lambda with receiver"Let's start with a normal function as a counter-example:Fun Alphabet
no arguments. The right side is the exact content of the expression, which is divided into simple expressions and blocks of code that contain complex logic. A simple expression can contain only a single expression, but a block of code is an anonymous block of code that can contain more complex logic.Let's take a look at two examples of simple expressions.(), "Hello, Lambda"This simple example does not acce
Java lambda expressions (1)Basic usage of lambda expressions
The callback and the lambda expression of Java 8 demonstrate the basic purpose of the lambda expression of Java 8: to comple
, for classes and arrays, there are two ways to construct a method reference. Constructs a method reference to use as a factory object. The following table summarizes all of the five method references:
method Reference type
example
equivalent lambda
Static
integer::p arseint
str-integer.parseint (str)
Bound
instant . N
here:
public static void Printpersonswithpredicate (
list
Use the predicate instance tester in this method to examine each person instance in the roster. If the person instance conforms to the check criteria defined in tester, the Printperson method of the person instance is triggered.
In addition to triggering the Printperson method, the person instance that satisfies the tester standard can perform other methods. You can consider using a
Anonymous implementation is achieved by a sentence, written below to better understand
Runnable r = ()-> System.out.println (Thread.CurrentThread (). GetId ());New Thread (R). Start ();
Of course, the purpose of LAMBDA is not only to write concise, a higher level of purpose and so on to conclude.
Look at an example of a comparator, according to the traditional wording, as follows:
Integer[] A =
needs to be mastered is to understand how the collection object is traversed internally, which is different from the currently existing external sequential traversal.
In this article, we'll show you the motivation behind the lambda expression, the application example, and, of course, its syntax.
Why do you need lambda expressions
There are three main reasons pro
);} conclusion 1. Lambda is a function. 2. Function interface 3 used by the new API defined by Java 8, default function method 4, which can be used to reference the method 5, this article describes how to use its sorting function to implement the sorting lambda application. First, I will give an example and then analyz
Syntax description
A lambda expression consists of the following parts:
1. A comma-delimited list of formal parameters in parentheses. The Checkperson.test method contains an argument p that represents an instance of the person class. Note: The type of the parameter in a lambda expression can be omitted, and parentheses can be omitted if there is only one argument. For
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.