两种 list转map方式
方式一:这种方式 重复key的话会报错 Duplicate key xxx
Map deviceDtoMap = JsonUtils.toList(deviceDtoList).stream().collect(Collectors.toMap(DeviceDto::getDeviceId, deviceDto -> deviceDto));
方式二:
Map<String, String> map = deviceDtoList.stream().collect(Collectors.toMap(DeviceDto :: getDeviceId, DeviceDto :: getDeviceId, (value1, value2) -> value2));
方式三:
Map<String, DeviceDto> deviceDtoMap = deviceDtoList.stream().collect(Collectors.toMap(DeviceDto::getDeviceId, Function.identity(),(entity1, entity2) -> entity1));
更详细原文:JDK8中Collectors.toMap方法报Duplicate key xxx错误信息_张志翔的博客-CSDN博客_collectors.tomap value是集合