Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName,(oldValue, newValue) -> newValue));
toMap()最少接收两个参数
这个方法碰到重复的key会抛出异常,把张三和李四的id改成一样,再次运行机会抛出异常
所以使用这个方法时需要注意,这时可以通过第三个参数来解决重复key的问题
Map<Integer, ProductCategoryResultDTO> resultDTOMap = byCategoryFidAndEnvironmentTag.stream()
.collect(Collectors.toMap(ProductCategory::getCategoryId, v -> {
ProductCategoryResultDTO productCategoryResultDTO = new ProductCategoryResultDTO();
productCategoryResultDTO.setStatus(v.getUplowState());
return productCategoryResultDTO;
}, (key1, key2) -> key2));
此时key2的值可以覆盖key1