Java8中Lambda表達(dá)式使用和Stream API詳解
前言
Java8 的新特性:Lambda表達(dá)式、強(qiáng)大的 Stream API、全新時(shí)間日期 API、ConcurrentHashMap、MetaSpace??偟脕碚f,Java8 的新特性使 Java 的運(yùn)行速度更快、代碼更少、便于并行、最大化減少空指針異常。
0x00. 前置數(shù)據(jù)
private List<People> peoples = null;
@BeforeEach void before () {
peoples = new ArrayList<>();
peoples.add(new People("K.O1", 21, new Date()));
peoples.add(new People("K.O3", 23, new Date()));
peoples.add(new People("K.O4", 24, new Date()));
peoples.add(new People("K.O5", 25, new Date()));
peoples.add(new People("K.O2", 22, new Date()));
peoples.add(new People("K.O6", 26, new Date()));
}
0x01. 提取對(duì)象中的一列
/**
* 提取1列
*/
@Test void whenExtractColumnSuccess () {
//第一種寫法
List<Integer> ages1 = peoples.stream().map(people -> people.getAge()).collect(Collectors.toList());
System.out.println("###println: args1----");
ages1.forEach(System.out::println);
//簡(jiǎn)單一點(diǎn)的寫法
List<Integer> ages2 = peoples.stream().map(People::getAge).collect(Collectors.toList());
System.out.println("###println: args2----");
ages1.forEach(System.out::println);
}
###println: args1----
21
22
23
24
25
26
###println: args2----
21
22
23
24
25
26
/**
* 只要年紀(jì)大于25歲的人
*/
@Test void whenFilterAgeGT25Success () {
List<People> peoples1 = peoples.stream().filter(x -> x.getAge() > 25).collect(Collectors.toList());
peoples1.forEach(x -> System.out.println(x.toString()));
}
People{name='K.O6', age=26, birthday=Wed May 15 22:20:22 CST 2019}
0x03. 列表中對(duì)象數(shù)值型列數(shù)據(jù)求和
/**
* 求和全部年紀(jì)
*/
@Test void sumAllPeopleAgeSuccess () {
Integer sum1 = peoples.stream().collect(Collectors.summingInt(People::getAge));
System.out.println("###sum1: " + sum1);
Integer sum2 = peoples.stream().mapToInt(People::getAge).sum();
System.out.println("###sum2: " + sum2);
}
###sum1: 141
###sum2: 141
0x04. 取出集合符合條件的第一個(gè)元素
/**
* 取出年紀(jì)為25歲的人
*/
@Test void extractAgeEQ25Success () {
Optional<People> optionalPeople = peoples.stream().filter(x -> x.getAge() == 25).findFirst();
if (optionalPeople.isPresent()) System.out.println("###name1: " + optionalPeople.get().getName());
//簡(jiǎn)寫
peoples.stream().filter(x -> x.getAge() == 25).findFirst().ifPresent(x -> System.out.println("###name2: " + x.getName()));
}
###name1: K.O5
###name2: K.O5
0x05. 對(duì)集合中對(duì)象字符列按規(guī)則拼接
/**
* 逗號(hào)拼接全部名字
*/
@Test void printAllNameSuccess () {
String names = peoples.stream().map(People::getName).collect(Collectors.joining(","));
System.out.println(names);
}
K.O1,K.O2,K.O3,K.O4,K.O5,K.O6
0x06. 將集合元素提取,轉(zhuǎn)為Map
/**
* 將集合轉(zhuǎn)成(name, age) 的map
*/
@Test void list2MapSuccess () {
Map<String, Integer> map1 = peoples.stream().collect(Collectors.toMap(People::getName, People::getAge));
map1.forEach((k, v) -> System.out.println(k + ":" + v));
System.out.println("--------");
//(name object)
Map<String, People> map2 = peoples.stream().collect(Collectors.toMap(People::getName, People::getThis));
map2.forEach((k, v) -> System.out.println(k + ":" + v.toString()));
}
//People中自己實(shí)現(xiàn)的方法
public People getThis () {
return this;
}
K.O2:22
K.O3:23
K.O1:21
K.O6:26
K.O4:24
K.O5:25
--------
K.O2:People{name='K.O2', age=22, birthday=Wed May 15 22:42:39 CST 2019}
K.O3:People{name='K.O3', age=23, birthday=Wed May 15 22:42:39 CST 2019}
K.O1:People{name='K.O1', age=21, birthday=Wed May 15 22:42:39 CST 2019}
K.O6:People{name='K.O6', age=26, birthday=Wed May 15 22:42:39 CST 2019}
K.O4:People{name='K.O4', age=24, birthday=Wed May 15 22:42:39 CST 2019}
K.O5:People{name='K.O5', age=25, birthday=Wed May 15 22:42:39 CST 2019}
0x07. 按集合某一屬性進(jìn)行分組
/**
* 按名字分組
*/
@Test void listGroupByNameSuccess() {
//添加一個(gè)元素方便看效果
peoples.add(new People("K.O1", 29, new Date()));
Map<String, List<People>> map = peoples.stream().collect(Collectors.groupingBy(People::getName));
map.forEach((k, v) -> System.out.println(k + ":" + v.size()));
}
K.O2:1
K.O3:1
K.O1:2
K.O6:1
K.O4:1
K.O5:1
0x08. 求集合對(duì)象數(shù)值列平均數(shù)
/**
* 求人平均年齡
*/
@Test void averagingAgeSuccess () {
Double avgAge = peoples.stream().collect(Collectors.averagingInt(People::getAge));
System.out.println(avgAge);
}
23.5
0x09. 對(duì)集合按某一列排序
/**
* 按年齡排序
*/
@Test void sortByAgeSuccess () {
System.out.println("###排序前---");
peoples.forEach(x -> System.out.println(x.getAge()));
peoples.sort((x, y) -> {
if (x.getAge() > y.getAge()) {
return 1;
} else if (x.getAge() == y.getAge()) {
return 0;
}
return -1;
});
System.out.println("###排序后---");
peoples.forEach(x -> System.out.println(x.getAge()));
}
###排序前---
21
23
24
25
22
26
###排序后---
21
22
23
24
25
26
未完待續(xù)
<源碼地址:https://github.com/cos2a/learning-repo/tree/master/core-java8>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
- 一文帶你徹底了解Java8中的Lambda,函數(shù)式接口和Stream
- Java的Lambda表達(dá)式和Stream流的作用以及示例
- Java分析Lambda表達(dá)式Stream流合并分組內(nèi)對(duì)象數(shù)據(jù)合并
- Java中的lambda和stream實(shí)現(xiàn)排序
- Java詳細(xì)分析Lambda表達(dá)式與Stream流的使用方法
- 吊打Java面試官之Lambda表達(dá)式 Stream API
- 詳解Java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)的兩種方式
- Java實(shí)現(xiàn)樹形結(jié)構(gòu)的示例代碼
- Java樹形結(jié)構(gòu)數(shù)據(jù)生成導(dǎo)出excel文件方法記錄
- Java使用 Stream 流和 Lambda 組裝復(fù)雜父子樹形結(jié)構(gòu)
相關(guān)文章
SpringCloud微服務(wù)架構(gòu)升級(jí)匯總
這篇文章主要介紹了SpringCloud微服務(wù)架構(gòu)升級(jí)匯總,它提倡將單一應(yīng)用程序劃分成一組小的服務(wù),服務(wù)之間互相協(xié)調(diào)、互相配合,為用戶提供最終價(jià)值,需要的朋友可以參考下2019-06-06
mybatis-plus(insertBatchSomeColumn批量添加方式)
這篇文章主要介紹了mybatis-plus(insertBatchSomeColumn批量添加方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法
短信驗(yàn)證功能在各個(gè)網(wǎng)站應(yīng)用都非常廣泛,那么在springmvc中如何實(shí)現(xiàn)短信驗(yàn)證碼功能呢?今天小編抽時(shí)間給大家介紹下Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法,一起看看吧2016-09-09
SpringCloud服務(wù)之間Feign調(diào)用不會(huì)帶上請(qǐng)求頭header的解決方法
在Spring?Cloud中,使用Feign進(jìn)行服務(wù)之間的調(diào)用時(shí),默認(rèn)情況下是不會(huì)傳遞header的,這篇文章給大家介紹SpringCloud服務(wù)之間Feign調(diào)用不會(huì)帶上請(qǐng)求頭header的解決方法,感興趣的朋友一起看看吧2024-01-01
IDEA插件開發(fā)注冊(cè)菜單之向主菜單注冊(cè)菜單項(xiàng)目
這篇文章主要介紹了IDEA插件開發(fā)注冊(cè)菜單之向主菜單注冊(cè)菜單項(xiàng)目,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Java并發(fā)系列之ReentrantLock源碼分析
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之ReentrantLock源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
SpringMVC JSON數(shù)據(jù)交互及RESTful支持實(shí)現(xiàn)方法
這篇文章主要介紹了SpringMVC JSON數(shù)據(jù)交互及RESTful支持實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
spring mvc中@RequestBody注解的作用說明
這篇文章主要介紹了spring mvc中@RequestBody注解的作用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

