当前位置:网站首页>Lambda expression
Lambda expression
2022-06-13 00:24:00 【Survivors with dreams behind their backs】
List of articles
brief introduction
Lambda expression , Also called closure , It's a push Java 8 The most important new feature of the release .
Lambda Allows you to take a function as an argument to a method ( Function is passed into the method as an argument ).
Use Lambda Expression can make the code more concise and compact .
grammar
(parameters) -> expression or (parameters) ->{ statements; }
Here are lambda Important features of expressions :
- Optional type declaration : Parameter types do not need to be declared , Compiler can identify parameter values uniformly .
- Optional parameter parentheses : A parameter does not need to define parentheses , But multiple parameters need to define parentheses .
- Optional braces : If the body contains a statement , You don't need braces .
- Optional return key : If the body has only one expression return value, the compiler returns the value automatically , Braces need to specify that the expression returns a value .
Scope
lambda Expressions can only refer to tags final The outer local variable of , That is to say, it can't be in lambda Internal modification of local variables defined outside the domain , Otherwise it will compile incorrectly .
/**
* Study Lambda expression - Scope
*
* @author zhao.hualuo
* Create at 2021/5/14
*/
public class LambdaScopeDemo {
private final static String START = "hello,";
private final static String END = "!";
private static String START2 = "hello,";
private static String END2 = "!";
/**
* Change the calling method to verify the function
*/
public static void main(String[] args) {
scope1();
}
interface Scope{
/**
* Output information
* @param message Information
*/
void printMessage(String message);
}
/**
* call final Marked global variables , normal
*/
public static void scope1() {
Scope scopePrint = message -> System.out.println(START + message + END);
scopePrint.printMessage("zhao");
}
/**
* Call not final Marked global variables , normal
*/
public static void scope2() {
Scope scopePrint = message -> System.out.println(START2 + message + END2);
scopePrint.printMessage("qian");
}
/**
* Call not final Marked global variables , And variables may be modified , normal
*/
public static void scope3() {
Scope scopePrint = message -> System.out.println(START2 + message + END2);
scopePrint.printMessage("qian");
START2 = "hi,";
END2 = ".";
scopePrint.printMessage("sun");
}
/**
* Call not final Marked local variables , Run failed
* java: from lambda The local variable referenced by the expression must be the final variable or the actual final variable
*/
public static void scope4() {
String start = "hello,";
String end = ".";
Scope scopePrint = message -> System.out.println(start + message + end);
scopePrint.printMessage("qian");
start = "hi,";
end = ".";
}
}
example
/**
* Study Lambda expression
*
* @author zhao.hualuo
* Create at 2021/5/14
*/
public class LambdaDemo {
public static void main(String[] args) {
// Declare parameter types
MathOperation addition = (int a, int b) -> a + b;
// Do not declare parameter types
MathOperation subtraction = (a, b) -> a - b;
// Add return value
MathOperation multiplication = (int a, int b) -> {return a * b;};
// No return value
MathOperation division = (int a, int b) -> a / b;
System.out.println("6 + 2 = " + operateUtil(6,2,addition));
System.out.println("6 - 2 = " + operateUtil(6,2,subtraction));
System.out.println("6 * 2 = " + operateUtil(6,2,multiplication));
System.out.println("6 / 2 = " + operateUtil(6,2,division));
}
interface MathOperation {
/**
* Arithmetic operation interface
*
* @param a The first parameter
* @param b The second parameter
* @return The result of the calculation is
*/
int operation(int a, int b);
}
/**
* Calculated tool class
*
* @param a The first parameter
* @param b The second parameter
* @param operation Calculation method
* @return The result of the calculation is
*/
private static int operateUtil(int a, int b, MathOperation operation) {
return operation.operation(a, b);
}
}
Be careful
stay Lambda It is not allowed to declare a parameter or local variable with the same name as the local variable in an expression .
String first = “”;
Comparator comparator = (first, second) -> Integer.compare(first.length(), second.length()); // Compilation error
边栏推荐
- PMP registration conditions, time, cost, new version related information
- Transaction creation of btcd transaction process (I)
- [Error] invalid use of incomplete type 使用了未定义的类型
- 新增博客地址
- Explain bio, NiO, AIO in detail
- Solution to the problem of closing the watchdog of STM32 in the sleep mode (stop/standby)
- [LeetCode]26. Removes duplicates from a sorted array thirty-three
- MySql索引
- The difference between philosophy and Literature
- PMP training organization
猜你喜欢
3、 Storage system
【HCIE论述】组播IGMP-A
[vscode]todo tree a to-do plug-in
MySql索引
Will the salary increase after obtaining PMP certification?
KAUST:Deyao Zhu | 价值记忆图:基于离线强化学习的图结构世界模型
[GYCTF2020]Ezsqli --BUUCTF
[C] Inverts the binary of a decimal number and outputs it
Transaction creation of btcd transaction process (I)
PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
随机推荐
【Matlab】基础知识
Will the salary increase after obtaining PMP certification?
2022 constructor - Equipment direction - General Foundation (constructor) operation certificate examination questions and simulation examination
新增博客地址
Using STM32 DMA to drive 4-wire SPI interface OLED high-speed display
TypeError: wave.ensureState is not a function
测试平台系列(97) 完善执行case部分
Several interview questions in TCP three grips and four swings
6.824 Lab 3B: Fault-tolerant Key/Value Service
Delphi2009 connecting Oracle11g
MAYA建模六
The PMP examination time in March 2022 is set -- "March 27"
Is the newly graduated college student taking BEC or PMP? PM who wants to transfer to another job in the future
Generate two-dimensional code in Delphi
Also on STM32 using dma+ serial port to obtain variable length data
BUUCTF之BabySQL[极客大挑战 2019]
【Matlab】基础运算
Go implements concurrent non blocking caching
浏览器缓存的执行流程
Test platform series (97) perfect the case part