Java lambda list轉(zhuǎn)換map時(shí),把多個(gè)參數(shù)拼接作為key操作
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
Map<String, Parts> partsMap = synList.stream().collect(Collectors.toMap(k ->
k.getOe()+k.getOeId()+k.getPartGroupId()+k.getStdPartId()+k.getBrandCode(), part -> part));
補(bǔ)充知識(shí):Java8 Collectors.toMap的兩個(gè)大坑
Collectors.toMap()方法的正常使用示例
List<StudentDTO> studentDTOS = Lists.newArrayList();
studentDTOS.add(new StudentDTO(1,"xixi"));
studentDTOS.add(new StudentDTO(2,"houhou"));
studentDTOS.add(new StudentDTO(3,"maomi"));
Map<Integer, String> collect = studentDTOS.stream().collect(
Collectors.toMap(StudentDTO::getStudentId, StudentDTO::getStudentName));
System.out.println(JSON.toJSON(collect)); // {"1":"xixi","2":"houhou","3":"maomi"}
一. 坑1:Duplicate Key時(shí)拋出IllegalStateException異常
1. 概述
按照常規(guī)Java的Map思維,往一個(gè)map里put一個(gè)已經(jīng)存在的key,會(huì)把原有的key對(duì)應(yīng)的value值覆蓋。
但Java8中的Collectors.toMap()卻不是這樣。當(dāng)key重復(fù)時(shí),該方法默認(rèn)會(huì)拋出IllegalStateException異常。
2. 大坑復(fù)現(xiàn)
public void streamToMap1() {
List<StudentDTO> studentDTOS = Lists.newArrayList();
studentDTOS.add(new StudentDTO(1,"xixi"));
studentDTOS.add(new StudentDTO(1,"houhou"));
studentDTOS.add(new StudentDTO(3,"maomi"));
Map<Integer, String> collect = studentDTOS.stream()
.collect(Collectors.toMap(StudentDTO::getStudentId, StudentDTO::getStudentName));
System.out.println(JSON.toJSON(collect));
}
輸出結(jié)果

3. 大坑解決
法1:將toMap方法修改成如下形式,這樣就可以使用新的value覆蓋原有value。
studentDTOS.stream().collect(Collectors.toMap(StudentDTO::getStudentId,
StudentDTO::getStudentName,(oldValue, newValue) -> newValue));
輸出結(jié)果:{"1":"houhou","3":"maomi"}
法2:如果需要保留同一個(gè)key下所有的值,則可以對(duì)value做簡(jiǎn)單的拼接,如下:
studentDTOS.stream().collect(Collectors.toMap(StudentDTO::getStudentId,
StudentDTO::getStudentName,(oldValue, newValue) -> oldValue + "," + newValue));
輸出結(jié)果:
{"1":"xixi,houhou","3":"maomi"}
二. 坑2:value為空時(shí)拋出NullPointerException異常
1. 概述
當(dāng)要轉(zhuǎn)化的map的value值中包含空指針時(shí), 會(huì)拋出NullPointerException異常。
2. 大坑復(fù)現(xiàn)
public void streamToMap2() {
List<StudentDTO> studentDTOS = Lists.newArrayList();
studentDTOS.add(new StudentDTO(1,"xixi"));
studentDTOS.add(new StudentDTO(2,"houhou"));
studentDTOS.add(new StudentDTO(3,null));
Map<Integer, String> collect = studentDTOS.stream().collect(Collectors
.toMap(StudentDTO::getStudentId, StudentDTO::getStudentName));
System.out.println(JSON.toJSON(collect));
}
輸出結(jié)果

3. 大坑解決
3.1 法1:value值判空設(shè)置
說(shuō)明:如果是null,則設(shè)置成一個(gè)特定值。
studentDTOS.stream().collect(Collectors.toMap(StudentDTO::getStudentId, studentDTO
-> studentDTO.getStudentName()==null?"":studentDTO.getStudentName()));
輸出結(jié)果:
{"1":"xixi","2":"houhou","3":""}
3.2 法2:使用collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner)方法構(gòu)建
說(shuō)明:該方法允許null值。
Map<Integer, String> collect = studentDTOS.stream().collect(HashMap::new,
(n, v) -> n.put(v.getStudentId(), v.getStudentName()), HashMap::putAll);
for(Map.Entry<Integer, String> entry:collect.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue());
}
輸出結(jié)果
1=xixi 2=houhou 3=null
3.3 使用Optional對(duì)值進(jìn)行包裝
Map<Integer, Optional<String>> collect = studentDTOS.stream().collect(Collectors
.toMap(StudentDTO::getStudentId,
studentDTO -> Optional.ofNullable(studentDTO.getStudentName())));
for(Map.Entry<Integer, Optional<String>> entry:collect.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue().orElse(""));
}
輸出結(jié)果
1=xixi 2=houhou 3=
以上這篇Java lambda list轉(zhuǎn)換map時(shí),把多個(gè)參數(shù)拼接作為key操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)手寫(xiě)線程池的示例代碼
在我們的日常的編程當(dāng)中,并發(fā)是始終離不開(kāi)的主題,而在并發(fā)多線程當(dāng)中,線程池又是一個(gè)不可規(guī)避的問(wèn)題。本文就來(lái)分享一下如何自己手寫(xiě)一個(gè)線程池,需要的可以參考一下2022-08-08
Java?FTP協(xié)議實(shí)現(xiàn)文件下載功能
FTP(File?Transfer?Protocol)就是文件傳輸協(xié)議。通過(guò)FTP客戶端從遠(yuǎn)程FTP服務(wù)器上拷貝文件到本地計(jì)算機(jī)稱為下載,將本地計(jì)算機(jī)上的文件復(fù)制到遠(yuǎn)程FTP服務(wù)器上稱為上傳,上傳和下載是FTP最常用的兩個(gè)功能2022-11-11
Java Spring JdbcTemplate基本使用詳解
JDBC已經(jīng)能夠滿足大部分用戶最基本的需求,但是在使用JDBC時(shí),必須自己來(lái)管理數(shù)據(jù)庫(kù)資源如:獲取PreparedStatement,設(shè)置SQL語(yǔ)句參數(shù),關(guān)閉連接等步驟2021-10-10
Java中如何取出String字符串括號(hào)中的內(nèi)容
這篇文章主要介紹了Java中如何取出String字符串括號(hào)中的內(nèi)容問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
詳解如何在SpringBoot項(xiàng)目中使用全局異常處理
在完整的項(xiàng)目開(kāi)發(fā)中,異常的出現(xiàn)幾乎是無(wú)法避免的;如果凡是有可能出現(xiàn)異常的地方,我們都手動(dòng)的使用try-catch將其捕獲的話,會(huì)使得代碼顯得十分臃腫并且后期不好維護(hù)。本文介紹了pringBoot項(xiàng)目中使用全局異常處理的方法,需要的可以參考一下2022-10-10
Springboot重寫(xiě)addInterceptors()方法配置攔截器實(shí)例
這篇文章主要介紹了Springboot重寫(xiě)addInterceptors()方法配置攔截器實(shí)例,spring?boot拋棄了復(fù)雜的xml配置,我們可以自定義配置類(標(biāo)注@Configuration注解的類)來(lái)實(shí)現(xiàn)WebMvcConfigurer接口,并重寫(xiě)addInterceptors()方法來(lái)配置攔截器,需要的朋友可以參考下2023-09-09
Java實(shí)現(xiàn)經(jīng)典大富翁游戲的示例詳解
大富翁,又名地產(chǎn)大亨。是一種多人策略圖版游戲。參與者分得游戲金錢(qián),憑運(yùn)氣(擲骰子)及交易策略,買(mǎi)地、建樓以賺取租金。本文將通過(guò)Java實(shí)現(xiàn)這一經(jīng)典游戲,感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-02-02
SpringMVC?RESTFul實(shí)現(xiàn)列表功能
這篇文章主要為大家介紹了SpringMVC?RESTFul實(shí)現(xiàn)列表功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

