假如下面的集合,转Map
List entityList = new ArrayList<>();
一、
Map<String, Entity> map = entityList.stream().collect(Collectors.toMap(entity->getId, entity->entity));
二、Map<String, Entity> map = entityList.stream().collect(Collectors.toMap(entity->getId, Function.identity()));
Function.identity() 与 entity->entity 相同 表示本体
三、Map<String, Entity> map = entityList.stream().collect(Collectors.toMap(Entity::getId, Function.identity()));
Entity::getId lambda表达式写法
四、
Map<String, Entity> map = entityList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));
(key1, key2) -> key2防止key重复
Stream学习笔记
最新推荐文章于 2022-07-04 14:49:20 发布