Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
How Lambda expressions are used
-
A Lambda expression is an anonymous function, a simplification of an anonymous inner class
-
There are no parameters and return values. You only need to write the function body in {}. The function body can be an expression or a statement block
-
“()” is used to call the expression
-
Lambda expressions with parameters and return values need to specify parameter names and parameter types, separated by English commas (,), and parameter types can be omitted. The function will calibrate automatically. – > indicates an arrow that points to parameters or data.
-
You can also assign a Lambda expression to a variable in development and call it directly from the variable
-
Do not place statements that are not return values at the position of the last statement in the method body
The concept and usage of higher order functions
-
If a function has only one argument and the argument type is a function type, the parentheses following the function name can be removed when the function is called
-
If a function has multiple arguments, but the last argument type is a function type, then the function can be called by removing the last argument from the parentheses and removing the symbol “, “between the arguments.
-
No matter how many arguments a function contains, if any of the arguments are of function type and the function type accepts only one argument, you can use the IT keyword instead of the parameter and arrow of the function
-
Functions can be used not only as arguments, but also as return values
- The find() method is used to find and return the first element of the specified condition, or NULL if no element is found
- The first() method is used to find and return the first element of the specified condition, and the last() method is used to find and return the last element of the specified condition. If the two methods do not find a match, they will throw an exception at run time
- The single() method is used to find an element in the current collection that meets the specified criteria. Note that there can only be one element that meets this condition, and multiple or none will throw an exception
- Use the takeWhile() method to find multiple elements that meet the criteria. When the takeWhile() method is called, the matching criteria must be the first element that meets the criteria before continuing the search
- Use the filter() method to print all elements that satisfy the criteria
- The count() method is used to find the number of elements that satisfy the current condition
- The maxBy() method is used to get the maximum value in the collection, the minBy() method is used to get the minimum value in the collection, and the distinctBy() method is used to remove duplicate elements in the collection
- The repeat() function is used to execute a statement repeatedly
- The run() function adds data to the ArrayList collection by omitting the current list object. Run () returns the last statement in the function body
- You can terminate the current run() function by using return@run
- All methods in the Standard class can terminate the current method with a “return@method name” format
How to use inline functions
- Lambda (functions) that are inline are called inline functions. Using inline functions can reduce the memory consumption of a program rather than inlining a function with a complex function
- Lambda expressions decorated with noinline are non-inline and can be used as arguments
Once studied Kotlin notes, transplantation.