Original: http://jobar.iteye.com/blog/202347710 examples of lambda expressions in Java8Example 1 implementing the Runnable interface with a lambda expressionJava Code Collection CodeBefore Java 8:New Thread (New Runnable () {@Overridepublic void Run () {System.out.println ("
specify the type. 3 Action dummyLambda = () => 4 { 5 Console.WriteLine("Hello World from a Lambda expression!"); 6 }; 7 8 // Can be used as with double y = square(25); 9 Func
From these statements, we can directly understand:
If there is only one input parameter, parentheses can be omitted.
If there is only one line of statement, and return in this statement, the braces can be omitted, an
element in a group of source data. For example, consider the following Delegate types:C #
Copy
public delegate TResult Func
The delegate can be instantiatedFunc, WhereintIs input parameter,boolIs the return value. The return value is always specified in the last type parameter.FuncThe definition contains two input parameters (intAndstring) And the return type isbool. When the followingFuncDuring delegation, the delegate returns true or false to indicate whether the input parameter is equal to 5
code is implemented in C + +: vectorCompared to the implementation of Python, because of the lack of chained calls, it seems that the C + + implementation version does not simplify much of the application logic, but looks slightly cluttered. But this does not prevent us from using lambda expressions where appropriate to optimize our code structure.3. Other languages and
parameter is equal to 5:
Func
When the parameter type isExpression You can also provide lambda expressions, such as the standard query operators defined in system. LINQ. queryable. If you specifyExpression Parameter, Lambda will compile as the expression directory tree.
A standard query operator is displayed here,
Method:
int[
statement block.
· The body of a Lambda expression can be an expression or a declaration block, while that of an anonymous function can only be a declaration block.
· Lambda expressions passed as arguments participant ipate in type argument inference (§ 26. 3.2) and in method overload resolution (§ 26. 3.3 ).
· The Lambda
accomplish the same task. Example 1: Using lambda This example passes a lambda to the For_each function. The lambda prints a result that indicates whether each element in the vector object is an even or an odd number. Code
Even_lambda.cpp
//compile WITH:CL/EHSC/NOLOGO/W4/MTD
#include
Output
1 is even
2 are odd
3 is even
4 are odd
5
expressions in listHere's a quick introduction to what a lambda expression is, and here's an example to learn more about lambda expressions.In the previous article, we also mentioned the List Public classbook{ Public stringName {Get;Set; } Public intYear {Get;Set; }}classprogram{Static voidMain (string[] args) { varBooks =NewList { NewB
storage duration, you can use a variable with a static storage duration in the body of a lambda expression. The following example uses the Generate function and the lambda expression to assign values to each element in a vector object. A lambda expression modifies a static variable to produce the value of the next element.
void Fillvector (vector
Th
previous example.
Imagine how to change the lambda expression in the getindexbylambda Method to the delegate format in. NET 2.0:
Static Dictionary Char , List String > Getindexbydelegate ( Ienumerable String > Keywords ){ Return Keywords. groupby ( Delegate ( String K ){ Return K [0];}). todictionary ( Delegate ( Igrouping Char , String > G ){ Return G. Key ;}, Delegate ( Igrouping Char , String > G ){ Return G. orderby ( Delegate ( String S
));
Simplified completion. Now let's compare the simplified and simplified code before you feel
In contrast to the age of two students public
void sort (student[] students) {
//simplified pre-
arrays.sort (students, new comparator
Other effects
Lambda expressions can also be used to assign values.
Runnable r = ()-> System.out.println ("Hello Lambda");
; Integer.compare(stu1.age, stu2.age));}Simplify the finish! Now compare the simplified and simplified code before you feel//对比两个学生的年龄publicvoidsort(Student[] students) { //简化前 new Comparator@Override publicintcompare(Student stu1, Student stu2) { return Integer.compare(stu1.age, stu2.age); } }); //简化后 Arrays.sort(students, (stu1, stu2) -> Integer.compare(stu1.age, stu2.age));}Other effectsLambda expressions can
example.
Imagine how to change the lambda expression in the getindexbylambda Method to the delegate format in. NET 2.0:
static Dictionary
Are you willing to write such code?
Therefore, lambda expressions play a decisive role here. In fact, it is because of lambda expressions
button:
button.setOnAction(new EventHandler
When this button is clicked, the handle method is executed.
From the above examples, you can see that these operations require a large segment of code to process. Such complex processing is not easily understood by everyone. Therefore, a very important feature added to Java 8 is lambda
ilambdatest1{
void print (String s);
The Print method in the corresponding, then why can this corresponding. There is no explanation on the code. If the interface has only one method, you can also know that a lambda expression can only be mapped to that method, but if the interface has multiple methods how to determine it.
OK, lambda expressions are harsh on
. The parameter types are the same. Note that implicit types must be involved in type discrimination.C. The return type of the delegate must be the same as that of Lambda, whether it is an expression or a statement block.
4. Lambda expressions and Expression TreeThe typical Declaration Form of the Expression Tree is 1: func 2: expression
Here we needSystem. LINQ
-------------------------------------------
1. What is a Lambda expression?
A Lambda expression is an anonymous function that can contain expressions and statements and can be used to create a delegate or expression directory tree. All lambda expressions use the
its type is derived by the compiler. The advantage is that you do not need to write too long variable types.Lambda expressions advancedAt the end, let's look at some advanced usage of C ++ Lambda expressions.Lambda expressions are introduced mainly for functional programming. With Lambda
expressions is few. In the actual development through the query results or data sources to make method calls, so that more query operations. Because a lambda expression is an anonymous function that can be assigned to a delegate, many of the methods in the IEnumerable interface implement custom operations, conditions, and so on through function delegates, so lambda
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.