亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java lambda list轉(zhuǎn)換map時,把多個參數(shù)拼接作為key操作

 更新時間:2020年08月22日 14:55:36   作者:feiwuguohai  
這篇文章主要介紹了Java lambda list轉(zhuǎn)換map時,把多個參數(shù)拼接作為key操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧~

Map<String, Parts> partsMap = synList.stream().collect(Collectors.toMap(k ->

k.getOe()+k.getOeId()+k.getPartGroupId()+k.getStdPartId()+k.getBrandCode(), part -> part));

補充知識:Java8 Collectors.toMap的兩個大坑

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時拋出IllegalStateException異常

1. 概述

按照常規(guī)Java的Map思維,往一個map里put一個已經(jīng)存在的key,會把原有的key對應的value值覆蓋。

但Java8中的Collectors.toMap()卻不是這樣。當key重復時,該方法默認會拋出IllegalStateException異常。

2. 大坑復現(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:如果需要保留同一個key下所有的值,則可以對value做簡單的拼接,如下:

studentDTOS.stream().collect(Collectors.toMap(StudentDTO::getStudentId,

StudentDTO::getStudentName,(oldValue, newValue) -> oldValue + "," + newValue));

輸出結(jié)果:

{"1":"xixi,houhou","3":"maomi"}

二. 坑2:value為空時拋出NullPointerException異常

1. 概述

當要轉(zhuǎn)化的map的value值中包含空指針時, 會拋出NullPointerException異常。

2. 大坑復現(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值判空設置

說明:如果是null,則設置成一個特定值。

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)建

說明:該方法允許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對值進行包裝

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ù)拼接作為key操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論