Java中Lambda表達(dá)式用法介紹
Lambda
lambda是一個匿名函數(shù),我們可以把lambda表達(dá)式理解為是一段可以傳遞的代碼。
- lambda簡明的地將代碼或方法作為參數(shù)傳遞進(jìn)去執(zhí)行。
- “函數(shù)式編程”其核心是把函數(shù)作為值。
- 函數(shù)式接口 :只有一個 抽象方法的接口 稱之為 函數(shù)式接口。函數(shù)式接口可以使用@FunctionalInterface進(jìn)行注解。
lambda表達(dá)式拆分為兩部分
左側(cè):lambda 表達(dá)式的參數(shù)列表
右側(cè):lambda 表達(dá)式中所需要執(zhí)行的功能,即lambda體
語法格式一:無參數(shù),無返回值
@Test
public void test(){
// () -> System.out.println("Hello");
Runnable a = new Runnable(){
@Override
public void run(){
System.out.println("Hello")
}
};
//等同于
Runnable a1 = () -> System.out.println("Hello");
a1.run();
}
語法格式二:有一個參數(shù),無返回值(若只有一個參數(shù) 小括號可以省略不寫)
@Test
public void test(){
//Consumer被注解@FunctionalInterface的接口(函數(shù)式接口) 唯一抽象方法 void accept(T t);
//左側(cè)參數(shù) -> 右側(cè)執(zhí)行體
Consumer<String> con = (x) -> System.out.println(x);
// x -> System.out.println(x);
con.accept("hahah");
}
語法格式三:有兩個以上的參數(shù),并且lambda體中有多條語句 (若lambda體中只有一條語句,return 和 大括號都可以省略不寫)
@Test
public void test(){
//Comparator被注解@FunctionalInterface的接口 舉例抽象方法 int compare(T o1,T o2);
Comparator<Integer> com = (x,y) -> {
System.out.println("hhaha0");
return (x < y) ? -1 : ((x == y) ? 0 : 1);
};
com.compare(1,2);
}
注意:lambda表達(dá)式的參數(shù)類型可以省略不寫,因?yàn)閖vm編譯器可以從上下文推斷出數(shù)據(jù)類型。即“類型推斷”如果要在參數(shù)里面寫數(shù)據(jù)類型,都要寫上。
實(shí)例
實(shí)例1:
class Employee {
private String name;
private int age;
private double salary;
?
//省略 get and set and constructor
?
}
interface MyPredicate<T> {
boolean test(T t);
}
public class Test{
static List<Employee> list = Arrays.asList(
new Employee("張三",10,1),
new Employee("里斯",20,1),
new Employee("王五",16,1),
new Employee("二三",30,1)
);
public static List<Employee> filterEmployee(List<Employee> list,MyPredicate<Employee> mp){
List<Employee> emps = new ArrayList<>();
for (Employee employee : list) {
if(mp.test(employee)){
emps.add(employee);
}
}
return emps;
}
@org.junit.Test
public void test1(){
//需要使用自定義的方法
List<Employee> list2 = filterEmployee(list,(e) -> e.getAge() >= 15);
list2.stream().map(Employee::getName).forEach(System.out::println);
}
@org.junit.Test
public void test2(){
//可以使用stream進(jìn)行l(wèi)ist集合的過濾 不使用自定義接口
List<Employee> list2 = list.stream().filter((e) -> e.getAge() >= 15).collect(Collectors.toList());
list2.stream().map(Employee::getName).forEach(System.out::println);
}
}
實(shí)例2:
創(chuàng)建一個MyFun接口使用@FunctionalInterface注解,并創(chuàng)建一個抽象方法Integer getValue(Integer num);在Test類對變量進(jìn)行某種操作。
@FunctionalInterface
interface MyFun{
Integer getValue(Integer num);
}
public class Test{
@org.junit.Test
public void Test(){
operation(100,num -> ++num);
}
/**
* param1 num : 傳入的整形數(shù)
* param2 mf : 實(shí)現(xiàn)某種方式對 整形數(shù) 進(jìn)行操作。
**/
public Integer operation(Integer num,MyFun mf){
return mf.getValue(num);
}
}
class Employee {
private String name;
private int age;
private double salary;
?
@Override
public String toString() {
return "["+this.name+","+this.getAge()+","+this.getSalary()+"]";
}
//省略 getter and setter and constructor
}
?
public class Test {
List<Employee> list = Arrays.asList(
new com.bilibili.lambda.test1.Employee("張三",10,1),
new com.bilibili.lambda.test1.Employee("里斯",20,1),
new com.bilibili.lambda.test1.Employee("王五",16,1),
new Employee("二三",30,1)
);
@org.junit.Test
public void test(){
Collections.sort(list,(e1,e2) -> {
if(e1.getAge() == e2.getAge()){
return e1.getName().compareTo(e2.getName());
}else{
//比較年齡大小
return Integer.compare(e1.getAge(),e2.getAge());
}
});
for (Employee e: list) {
System.out.println(e);
}
}
}
四大核心函數(shù)式接口
- Consumer<T> : 消費(fèi)性接口 void accept(T t);
- Supplier<T> : 共給性接口 T get();
- Function<T,R> : 函數(shù)性接口 T代表參數(shù),R代表返回值 R apply(T t);
- Predicate<T> :斷言性接口 boolean test(T t);
class Test{
@org.junit.Test
publilc void test(){
happy(10000,(money)->System.out.println("happy消費(fèi)"+money+"元"));
}
public void happy(double money,Consumer<double> con){
con.accept(money);
}
}
lambda方法引用
方法引用:若lambda體中的內(nèi)同有方法已經(jīng)實(shí)現(xiàn)了,我們可以使用“方法引用”
(可以理解為方法引用時lambda的另一種表現(xiàn)形式)
主要有三種語法格式:
- 對象::實(shí)例方法名
- 類::靜態(tài)方法名
- 類::實(shí)例方法名
class Test{
//對象::實(shí)例方法名
@org.junit.Test
public void test(){
Consumer<String> con = (x) -> System.out.println(x);
con.accept("haha");
Consumer<String> con2 = System.out::println;
con2.accept("haha");
}
//類::靜態(tài)方法名
@org.junit.Test
public void test2(){
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
Comparator<Integer> com2 = Integer::compare;
com.compare(1,2);
com2.compare(1,2);
}
//類::實(shí)例方法名
@org.junit.Test(){
BiPredicate<String,String> bp = (x,y) -> x.equals(y);
bp.test("a","a");
BiPredicate<String,String> bp2 = String::equals;
}
}
lambda構(gòu)造器引用
格式:
CalssName::new
class Test{
@org.junit.Test
public void test(){
Supplier<String> sup = () -> new String();
//這里的構(gòu)造器引用取決于 接口方法的參數(shù) 的個數(shù)。 此處函數(shù)式接口 T get(); 為無參抽象方法所以String在實(shí)例化時 也是實(shí)例化無參的構(gòu)造方法 其他類也適用
Supplier<String> sup2 = String::new;
String str = sup2.get();
}
}
到此這篇關(guān)于Java中Lambda表達(dá)式用法介紹的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringValidation自定義注解及分組校驗(yàn)功能詳解
這篇文章主要介紹了SpringValidation自定義注解及分組校驗(yàn)功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
JavaWeb實(shí)現(xiàn)多文件上傳及zip打包下載
這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)多文件上傳及zip打包下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Java實(shí)現(xiàn)上傳Excel文件并導(dǎo)入數(shù)據(jù)庫
這篇文章主要介紹了在java的基礎(chǔ)上學(xué)習(xí)上傳Excel文件并導(dǎo)出到數(shù)據(jù)庫,感興趣的小伙伴不要錯過奧2021-09-09
詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序
本篇文章主要介紹了詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Springboot整合freemarker 404問題解決方案
這篇文章主要介紹了Springboot整合freemarker 404問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
Java多線程實(shí)現(xiàn)之Executor詳解
這篇文章主要介紹了Java多線程實(shí)現(xiàn)之Executor詳解,Executor 給他一個 Runnable,他就能自動很安全的幫你把這個線程執(zhí)行完畢2023-08-08
Executor 通過創(chuàng)建線程池的方式來管理線程,需要的朋友可以參考下
Java調(diào)用HTTPS接口實(shí)現(xiàn)繞過SSL認(rèn)證
SSL認(rèn)證是確保通信安全的重要手段,有的時候?yàn)榱朔奖阏{(diào)用,我們會繞過SSL認(rèn)證,這篇文章主要介紹了Java如何調(diào)用HTTPS接口實(shí)現(xiàn)繞過SSL認(rèn)證,需要的可以參考下2023-11-11

