当前位置:网站首页>Lambda expression
Lambda expression
2022-07-27 12:39:00 【Rippling rippling】
Why is there Lambda expression ?
Java Support Lambda Expression starts with Java 8, Its appearance simplifies the syntax of anonymous inner classes of functional interfaces
How do you use it? ?
1.
The expression syntax is as follows :([ Parameters 1], [ Parameters 2], [ Parameters 3],… [ Parameters n])->{ Code block }
example :
Look at the following main The content of the method :
// Anonymous inner class :
@FunctionalInterface
interface IComputer {
void add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = new IComputer() {
@Override
public void add(int a, int b) {
System.out.println(a + b);
}
};
computer.add(1, 1);
}
}
//Lambda expression :
@FunctionalInterface
interface IComputer {
void add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = (int a, int b)-> {
System.out.println(a + b);
};
computer.add(1, 1);
}
}
2.
If the method has no return value and only one line of code , be Lambda Expression syntax can be in this form :([ Parameters 1], [ Parameters 2], [ Parameters 3],… [ Parameters n])-> Single line statement , Here's an example :
// Anonymous inner class :
@FunctionalInterface
interface IMammal {
void move(String name);
}
public class Test {
public static void main(String[] args) {
IMammal whale = (name) -> {
System.out.println(name+" Moving ......");
};
whale.move(" Whale ");
}
}
//Lambda expression :
@FunctionalInterface
interface IMammal {
void move(String name);
}
public class Test {
public static void main(String[] args) {
IMammal whale = (name) -> System.out.println(name+" Moving ......");
whale.move(" Whale ");
}
}
3.
If the method has a return value and only one line of code , be Lambda Expression syntax can be in this form :([ Parameters 1], [ Parameters 2], [ Parameters 3],… [ Parameters n])-> expression , Here's an example :
// Anonymous inner class :
@FunctionalInterface
interface IComputer {
int add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = (a, b) -> {
return a+b;
};
int result = computer.add(1,2);
System.out.println(result);
}
}
//Lambda expression :
@FunctionalInterface
interface IComputer {
int add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = (a, b) -> a+b;
int result = computer.add(1,2);
System.out.println(result);
}
}
边栏推荐
- Interviewer: how to deal with the data loss of redis master-slave cluster switching?
- XXL job parameter transfer
- How to design a secure external interface
- NFT mall /nft blind box / virtual blind box /nft transaction / customizable second opening
- 2021-03-15
- 4. Analysis of the execution process of make modules for general purposes
- Soft core microprocessor
- Security measures for tcp/ip protocol vulnerabilities
- Detailed explanation of flask framework
- Julia beginner tutorial 2022
猜你喜欢

Interviewer: how to deal with the data loss of redis master-slave cluster switching?

The sparksubmit. Main () method submits external parameters and remotely submits the standalone cluster task

Fundamentals of mathematics 01

Xposed+FDex2 app脱壳 (黑猫投诉app脱壳)

Map接口

20210518-Cuda

A personal blog integrating technology, art and sports.

Dominoes staged: the beginning and end of the three arrow capital crash

About the problem that the onapplicationevent method of the custom listener is executed multiple times

Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
随机推荐
详述throw与throws
MySQL common commands
Top of the tide - reading notes + excerpts + insights
2021-4-7-find the maximum water holding capacity
Detailed explanation of flask framework
Redis data type
POJ1988_Cube Stacking
V. introduction of other objectives and general options
Set接口
Vi. analysis of makefile.build
Chapter 13 IO flow
关于 CMS 垃圾回收器,你真的懂了吗?
SSM实战项目-前后分离(简单易懂)
Julia beginner tutorial 2022
2021-3-17-byte-hu Pai
Soft core microprocessor
5V升压9V芯片
Photoshop web design tutorial
Time tool class, get the current time, date to string
What are metauniverse and NFT digital collections?