一、将List集合转换为Map
声明一个Entity类,用于后续举例操作
@Data
public class Entity {
private Long id;
private String name;
}
List<Entity> entityList = new ArrayList<>();
(1)将List集合元素中的两种属性分别转化为key-value的Map结构
Map<Long, String> entityIdToNameMap = entityList.stream().collect(Collectors.toMap(Entity::getId, Entity::getName));
(2)将List集合元素中的某一个属性作为key,List本身作为value的Map结构
Map<Long, List<Entity>> idToEntityListMap = entityList.stream().collect(Collectors.groupingBy(Entity::getId));
(3)将List集合元素中的某一个属性作为key,其本身作为value的Map结构
//此处的item -> item是lambda表达式,表示传入一个entity的对象,返回一个entity对象
Map<Long, Entity> idToEntityMap = entityList.stream().collect(Collectors.toMap(Entity:getId, item -> item));
二、将List集合中的属性抽取出转化成新的List
List<Long> idList = entityList.stream().map(Entity::getId).collect(Collectors.toList());
三、使用流示例
3.1 中间流,以下操作均为对流进行中间操作,并未终止流
(1)filter()
过滤
//过滤出id大于20的元素
entityList.stream().filter(item -> item.getId() > 20).forEach(System.out::println);
(2)limit()
截断流
//截断流,筛选出前三条
entityList.stream().limit(3).forEach(System.out::println);
(3) skip()
跳过
//跳过前2个元素
entityList.stream().skip(2).forEach(System.out.println);
(4) distinct()
去重
//去除重复元素
entityList.stream().distinct().forEach(System.out::println);
(5) map()
映射,接收lambda,将元素转换成其它形式或者提取信息,将其映射成一个新的元素
//将List<Entity> 转换成 List<Long>
entityList.stream().map(Entity::getId).collect(Collectors.toList());
(6) sorted()
自然排序、 sorted(Comparator)
定制排序
//自然排序,需要排序的对象实现了Comparator接口
List<Integer> list = Arrays.asList(4, 8, 9, 10);
list.stream().sorted().forEach(System.out::println);
//定制排序,按照id从小到大排序,当返回值为1时,会交换两个元素的位置,为0和-1都不会交换位置
entityList.stream().sorted((e1, e2) -> (int)(e2.getId() - e1.getId())).forEach(System.out::println);
3.2 终止流,以下操作使用后,流终止,得出结果
(1) allMatch()
检查流中所有元素是否满足条件,返回值时boolean
//判断所有元素的id是否都大于20
boolean allMatch = entityList.stream().allMatch(entity -> entity.getId() > 20);
(2) anyMatch()
检查流中是否有任意一个元素满足条件,返回值为boolean
//检查是否至少有一个元素满足id大于20的条件
boolean anyMath = entityList.stream().anyMatch(entity -> entity.getId > 20);
(3) noneMatch()
检查流中所有元素是否没有匹配指定条件,如果没有一个匹配成功,返回true,其实就是allMatch的取反操作
//判断是否没有一个元素的id < 20
boolean noneMatch = entityList.stream().noneMatch(entity -> entity.getId() < 20);
(4) findFirst()
返回第一个元素
//返回流中的第一个元素
Optional<Entity> first = entityList.stream().findFirst();
(5) findAny()
返回流中的任意一个元素
//返回流中的任意一个元素
Optional<Entity> any = entityList.stream().findAny();
(6) count()
返回流中元素的总个数
//返回流中元素的总个数
long count = entity.stream().count();
(7) max()
返回流中的最大值, min()
返回流中最小值
//查找所有元素中id最大的元素
Optional<Entity> maxIdEntity = entityList.stream().max((e1, e2) -> (int)(e1.getId() - e2.getId()));
//查找所有元素中id最小的元素
Optional<Entity> minIdEntity = entityList.stream().min((e1, e2) -> (int)(e1.getId() - e2.getId()));
(8) reduce()
规约操作可以将所有元素全部结合起来得到一个值
Optional<Long> reduceSum = entityList.stream().map(Entity::getId).reduce(Long::sum);
(9) collect()
收集操作,将流转换成其它形式,接收一个Collector接口实现,用于收集stream所有元素的方法
//返回一个List
List<Entity> list = entityList.stream().filter(entity -> entity.getId() > 20).collect(Collectors.toList());
//返回一个Set
Set<Entity> set = entityList.stream().filter(entity -> entity.getId() > 20).collect(Collectors.toSet());
//返回一个Map
Map<Long, Entity> map = entityList.stream().filter(entity -> entity.getId() > 20).collect(Collectors.toMap(Entity::getId, e -> e));