java常用Lambda表達(dá)式使用場(chǎng)景源碼示例
引導(dǎo)語
我們?nèi)粘9ぷ髦?,Lambda 使用比較多的場(chǎng)景,就是 List 或 Map 下的 Lambda 流操作,往往幾行代碼可以幫助我們實(shí)現(xiàn)多層 for 循環(huán)嵌套的復(fù)雜代碼,接下來我們把 Lambda 流的常用方法用案列講解一下。
1、數(shù)據(jù)準(zhǔn)備
本文演示的所有代碼都在 demo.eight.LambdaExpressionDemo 中,首先我們需要準(zhǔn)備一些測(cè)試的數(shù)據(jù),如下:
@Data // 學(xué)生數(shù)據(jù)結(jié)構(gòu) class StudentDTO implements Serializable { private static final long serialVersionUID = -7716352032236707189L; public StudentDTO() { } public StudentDTO(Long id, String code, String name, String sex, Double scope, List<Course> learningCources) { this.id = id; this.code = code; this.name = name; this.sex = sex; this.scope = scope; this.learningCources = learningCources; } /** * id */ private Long id; /** * 學(xué)號(hào) 唯一標(biāo)識(shí) */ private String code; /** * 學(xué)生名字 */ private String name; /** * 性別 */ private String sex; /** * 分?jǐn)?shù) */ private Double scope; /** * 要學(xué)習(xí)的課程 */ private List<Course> learningCources; } @Data // 課程數(shù)據(jù)結(jié)構(gòu) class Course implements Serializable { private static final long serialVersionUID = 2896201730223729591L; /** * 課程 ID */ private Long id; /** * 課程 name */ private String name; public Course(Long id, String name) { this.id = id; this.name = name; } } // 初始化數(shù)據(jù) private final List<StudentDTO> students = new ArrayList<StudentDTO>(){ { // 添加學(xué)生數(shù)據(jù) add(new StudentDTO(1L,"W199","小美","WM",100D,new ArrayList<Course>(){ { // 添加學(xué)生學(xué)習(xí)的課程 add(new Course(300L,"語文")); add(new Course(301L,"數(shù)學(xué)")); add(new Course(302L,"英語")); } })); add(new StudentDTO(2L,"W25","小美","WM",100D,Lists.newArrayList())); add(new StudentDTO(3L,"W3","小名","M",90D,new ArrayList<Course>(){ { add(new Course(300L,"語文")); add(new Course(304L,"體育")); } })); add(new StudentDTO(4L,"W1","小藍(lán)","M",10D,new ArrayList<Course>(){ { add(new Course(301L,"數(shù)學(xué)")); add(new Course(305L,"美術(shù)")); } })); } };
請(qǐng)大家稍微看下數(shù)據(jù)結(jié)構(gòu),不然看下面案例跑出來的結(jié)果會(huì)有些吃力。
2、常用方法
2.1、Filter
Filter 為過濾的意思,只要滿足 Filter 表達(dá)式的數(shù)據(jù)就可以留下來,不滿足的數(shù)據(jù)被過濾掉,源碼如下圖:
我們寫了一個(gè) demo,如下:
public void testFilter() { // list 在下圖中進(jìn)行了初始化 List<String> newList = list.stream() // 過濾掉我們希望留下來的值 // StringUtils.equals(str,"hello") 表示我們希望字符串是 hello 能留下來 // 其他的過濾掉 .filter(str -> StringUtils.equals(str, "hello")) // Collectors.toList() 幫助我們構(gòu)造最后的返回結(jié)果 .collect(Collectors.toList()); log.info("TestFilter result is {}", JSON.toJSONString(newList)); }
運(yùn)行結(jié)果如下:
2.2、map
map 方法可以讓我們進(jìn)行一些流的轉(zhuǎn)化,比如原來流中的元素是 A,通過 map 操作,可以使返回的流中的元素是 B,源碼如下圖:
我們寫了一個(gè) demo,如下:
public void testMap() { // 得到所有學(xué)生的學(xué)號(hào) // 這里 students.stream() 中的元素是 StudentDTO,通過 map 方法轉(zhuǎn)化成 String 的流 List<String> codes = students.stream() //StudentDTO::getCode 是 s->s.getCode() 的簡(jiǎn)寫 .map(StudentDTO::getCode) .collect(Collectors.toList()); log.info("TestMap 所有學(xué)生的學(xué)號(hào)為 {}", JSON.toJSONString(codes)); } // 運(yùn)行結(jié)果為:TestMap 所有學(xué)生的學(xué)號(hào)為 ["W199","W25","W3","W1"]
2.3、mapToInt
mapToInt 方法的功能和 map 方法一樣,只不過 mapToInt 返回的結(jié)果已經(jīng)沒有泛型,已經(jīng)明確是 int 類型的流了,源碼如下:
我們寫了一個(gè) demo,如下:
public void testMapToInt() { List<Integer> ids = students.stream() .mapToInt(s->Integer.valueOf(s.getId()+"")) // 一定要有 mapToObj,因?yàn)?mapToInt 返回的是 IntStream,因?yàn)橐呀?jīng)確定是 int 類型了 // 所有沒有泛型的,而 Collectors.toList() 強(qiáng)制要求有泛型的流,所以需要使用 mapToObj // 方法返回有泛型的流 .mapToObj(s->s) .collect(Collectors.toList()); log.info("TestMapToInt result is {}", JSON.toJSONString(ids)); // 計(jì)算學(xué)生總分 Double sumScope = students.stream() .mapToDouble(s->s.getScope()) // DoubleStream/IntStream 有許多 sum(求和)、min(求最小值)、max(求最大值)、average(求平均值)等方法 .sum(); log.info("TestMapToInt 學(xué)生總分為: is {}", sumScope); }
運(yùn)行結(jié)果如下:
TestMapToInt result is [1,2,3,4]
TestMapToInt 學(xué)生總分為: is 300.0
2.4、flatMap
flatMap 方法也是可以做一些流的轉(zhuǎn)化,和 map 方法不同的是,其明確了 Function 函數(shù)的返回值的泛型是流,源碼如下:
寫了一個(gè) demo,如下:
public void testFlatMap(){ // 計(jì)算學(xué)生所有的學(xué)習(xí)課程,flatMap 返回 List<課程> 格式 List<Course> courses = students.stream().flatMap(s->s.getLearningCources().stream()) .collect(Collectors.toList()); log.info("TestMapToInt flatMap 計(jì)算學(xué)生的所有學(xué)習(xí)課程如下 {}", JSON.toJSONString(courses)); // 計(jì)算學(xué)生所有的學(xué)習(xí)課程,map 返回兩層課程嵌套格式 List<List<Course>> courses2 = students.stream().map(s->s.getLearningCources()) .collect(Collectors.toList()); log.info("TestMapToInt map 計(jì)算學(xué)生的所有學(xué)習(xí)課程如下 {}", JSON.toJSONString(courses2)); List<Stream<Course>> courses3 = students.stream().map(s->s.getLearningCources().stream()) .collect(Collectors.toList()); log.info("TestMapToInt map 計(jì)算學(xué)生的所有學(xué)習(xí)課程如下 {}", JSON.toJSONString(courses3)); }
運(yùn)行結(jié)果如下:
2.5、distinct
distinct 方法有去重的功能,我們寫了一個(gè) demo,如下:
public void testDistinct(){ // 得到學(xué)生所有的名字,要求是去重過的 List<String> beforeNames = students.stream().map(StudentDTO::getName).collect(Collectors.toList()); log.info("TestDistinct 沒有去重前的學(xué)生名單 {}",JSON.toJSONString(beforeNames)); List<String> distinctNames = beforeNames.stream().distinct().collect(Collectors.toList()); log.info("TestDistinct 去重后的學(xué)生名單 {}",JSON.toJSONString(distinctNames)); // 連起來寫 List<String> names = students.stream() .map(StudentDTO::getName) .distinct() .collect(Collectors.toList()); log.info("TestDistinct 去重后的學(xué)生名單 {}",JSON.toJSONString(names)); }
運(yùn)行結(jié)果如下:
2.6、Sorted
Sorted 方法提供了排序的功能,并且允許我們自定義排序,demo 如下:
public void testSorted(){ // 學(xué)生按照學(xué)號(hào)排序 List<String> beforeCodes = students.stream().map(StudentDTO::getCode).collect(Collectors.toList()); log.info("TestSorted 按照學(xué)號(hào)排序之前 {}",JSON.toJSONString(beforeCodes)); List<String> sortedCodes = beforeCodes.stream().sorted().collect(Collectors.toList()); log.info("TestSorted 按照學(xué)號(hào)排序之后 is {}",JSON.toJSONString(sortedCodes)); // 直接連起來寫 List<String> codes = students.stream() .map(StudentDTO::getCode) // 等同于 .sorted(Comparator.naturalOrder()) 自然排序 .sorted() .collect(Collectors.toList()); log.info("TestSorted 自然排序 is {}",JSON.toJSONString(codes)); // 自定義排序器 List<String> codes2 = students.stream() .map(StudentDTO::getCode) // 反自然排序 .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); log.info("TestSorted 反自然排序 is {}",JSON.toJSONString(codes2)); }
運(yùn)行結(jié)果如下:
2.7、peek
peek 方法很簡(jiǎn)單,我們?cè)?peek 方法里面做任意沒有返回值的事情,比如打印日志,如下:
students.stream().map(StudentDTO::getCode) .peek(s -> log.info("當(dāng)前循環(huán)的學(xué)號(hào)是{}",s)) .collect(Collectors.toList());
2.8、limit
limit 方法會(huì)限制輸出值個(gè)數(shù),入?yún)⑹窍拗频膫€(gè)數(shù)大小,demo 如下:
public void testLimit(){ List<String> beforeCodes = students.stream().map(StudentDTO::getCode).collect(Collectors.toList()); log.info("TestLimit 限制之前學(xué)生的學(xué)號(hào)為 {}",JSON.toJSONString(beforeCodes)); List<String> limitCodes = beforeCodes.stream() .limit(2L) .collect(Collectors.toList()); log.info("TestLimit 限制最大限制 2 個(gè)學(xué)生的學(xué)號(hào) {}",JSON.toJSONString(limitCodes)); // 直接連起來寫 List<String> codes = students.stream() .map(StudentDTO::getCode) .limit(2L) .collect(Collectors.toList()); log.info("TestLimit 限制最大限制 2 個(gè)學(xué)生的學(xué)號(hào) {}",JSON.toJSONString(codes)); }
輸出結(jié)果如下:
2.9、reduce
reduce 方法允許我們?cè)谘h(huán)里面疊加計(jì)算值,我們寫了 demo 如下:
public void testReduce(){ // 計(jì)算一下學(xué)生的總分?jǐn)?shù) Double sum = students.stream() .map(StudentDTO::getScope) // scope1 和 scope2 表示循環(huán)中的前后兩個(gè)數(shù) .reduce((scope1,scope2) -> scope1+scope2) .orElse(0D); log.info("總成績(jī)?yōu)?{}",sum); Double sum1 = students.stream() .map(StudentDTO::getScope) // 第一個(gè)參數(shù)表示成績(jī)的基數(shù),會(huì)從 100 開始加 .reduce(100D,(scope1,scope2) -> scope1+scope2); log.info("總成績(jī)?yōu)?{}",sum1); }
運(yùn)行結(jié)果如下:
第二個(gè)計(jì)算出來的總成績(jī)多了 100,是因?yàn)榈诙€(gè)例子中 reduce 是從基數(shù) 100 開始累加的。
2.10、findFirst
findFirst 表示匹配到第一個(gè)滿足條件的值就返回,demo 如下:
// 找到第一個(gè)叫小美同學(xué)的 ID @Test public void testFindFirst(){ Long id = students.stream() .filter(s->StringUtils.equals(s.getName(),"小美")) // 同學(xué)中有兩個(gè)叫小美的,這里匹配到第一個(gè)就返回 .findFirst() .get().getId(); log.info("testFindFirst 小美同學(xué)的 ID {}",id); // 防止空指針 Long id2 = students.stream() .filter(s->StringUtils.equals(s.getName(),"小天")) .findFirst() // orElse 表示如果 findFirst 返回 null 的話,就返回 orElse 里的內(nèi)容 .orElse(new StudentDTO()).getId(); log.info("testFindFirst 小天同學(xué)的 ID {}",id2); Optional<StudentDTO> student= students.stream() .filter(s->StringUtils.equals(s.getName(),"小天")) .findFirst(); // isPresent 為 true 的話,表示 value != null,即 student.get() != null if(student.isPresent()){ log.info("testFindFirst 小天同學(xué)的 ID {}",student.get().getId()); return; } log.info("testFindFirst 找不到名為小天的同學(xué)"); }
運(yùn)行結(jié)果如下:
2.11、groupingBy && toMap
groupingBy 是能夠根據(jù)字段進(jìn)行分組,toMap 是把 List 的數(shù)據(jù)格式轉(zhuǎn)化成 Map 的格式,我們寫了一個(gè) demo,如下:
@Test public void testListToMap(){ // 學(xué)生根據(jù)名字進(jìn)行分類 Map<String, List<StudentDTO>> map1 = students.stream() .collect(Collectors.groupingBy(StudentDTO::getName)); log.info("testListToMap groupingBy 學(xué)生根據(jù)名字進(jìn)行分類 result is Map<String,List<StudentDTO>> {}", JSON.toJSONString(map1)); // 統(tǒng)計(jì)姓名重名的學(xué)生有哪些 Map<String, Set<String>> map2 = students.stream() .collect(Collectors.groupingBy(StudentDTO::getName, Collectors.mapping(StudentDTO::getCode,Collectors.toSet()))); log.info("testListToMap groupingBy 統(tǒng)計(jì)姓名重名結(jié)果 is {}", JSON.toJSONString(map2)); // 學(xué)生轉(zhuǎn)化成學(xué)號(hào)為 key 的 map Map<String, StudentDTO> map3 = students.stream() //第一個(gè)入?yún)⒈硎?map 中 key 的取值 //第二個(gè)入?yún)⒈硎?map 中 value 的取值 //第三個(gè)入?yún)⒈硎?,如果前后?key 是相同的,是覆蓋還是不覆蓋,(s1,s2)->s1 表示不覆蓋,(s1,s2)->s2 表示覆蓋 .collect(Collectors.toMap(s->s.getCode(),s->s,(s1,s2)->s1)); log.info("testListToMap groupingBy 學(xué)生轉(zhuǎn)化成學(xué)號(hào)為 key 的 map result is{}", JSON.toJSONString(map3)); }
運(yùn)行結(jié)果如下:
3、總結(jié)
本文我們介紹了 12 種 Lambda 表達(dá)式常用的方法,大家可以找到 LambdaExpressionDemo 類,自己 debug 下,這樣你在工作中遇到復(fù)雜數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)化時(shí),肯定會(huì)得心應(yīng)手了,希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis通過if語句實(shí)現(xiàn)增刪改查操作
這篇文章主要介紹了mybatis通過if語句實(shí)現(xiàn)增刪改查操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11通過簡(jiǎn)單步驟實(shí)現(xiàn)SpringMVC文件上傳
這篇文章主要介紹了通過簡(jiǎn)單步驟實(shí)現(xiàn)SpringMVC文件上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11MyBatis攔截器:給參數(shù)對(duì)象屬性賦值的實(shí)例
下面小編就為大家?guī)硪黄狹yBatis攔截器:給參數(shù)對(duì)象屬性賦值的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04SpringBoot在項(xiàng)目中訪問靜態(tài)資源步驟分析
今天在玩SpringBoot的demo的時(shí)候,放了張圖片在resources目錄下,啟動(dòng)區(qū)訪問的時(shí)候,突然好奇是識(shí)別哪些文件夾來展示靜態(tài)資源的, 為什么有時(shí)候放的文件夾不能顯示,有的卻可以2023-01-01spring boot實(shí)現(xiàn)驗(yàn)證碼功能
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了spring boot實(shí)現(xiàn)驗(yàn)證碼功能,需要的朋友可以參考下2018-04-04