一、将List集合转换为Map

声明一个Entity类,用于后续举例操作

  1. @Data
  2. public class Entity {
  3. private Long id;
  4. private String name;
  5. }
  6. List<Entity> entityList = new ArrayList<>();

(1)将List集合元素中的两种属性分别转化为key-value的Map结构

  1. Map<Long, String> entityIdToNameMap = entityList.stream().collect(Collectors.toMap(Entity::getId, Entity::getName));

(2)将List集合元素中的某一个属性作为key,List本身作为value的Map结构

  1. Map<Long, List<Entity>> idToEntityListMap = entityList.stream().collect(Collectors.groupingBy(Entity::getId));

(3)将List集合元素中的某一个属性作为key,其本身作为value的Map结构

  1. //此处的item -> item是lambda表达式,表示传入一个entity的对象,返回一个entity对象
  2. Map<Long, Entity> idToEntityMap = entityList.stream().collect(Collectors.toMap(Entity:getId, item -> item));

二、将List集合中的属性抽取出转化成新的List

  1. List<Long> idList = entityList.stream().map(Entity::getId).collect(Collectors.toList());

三、使用流示例

3.1 中间流,以下操作均为对流进行中间操作,并未终止流

(1)filter() 过滤

  1. //过滤出id大于20的元素
  2. entityList.stream().filter(item -> item.getId() > 20).forEach(System.out::println);

(2)limit() 截断流

  1. //截断流,筛选出前三条
  2. entityList.stream().limit(3).forEach(System.out::println);

(3) skip() 跳过

  1. //跳过前2个元素
  2. entityList.stream().skip(2).forEach(System.out.println);

(4) distinct()去重

  1. //去除重复元素
  2. entityList.stream().distinct().forEach(System.out::println);

(5) map() 映射,接收lambda,将元素转换成其它形式或者提取信息,将其映射成一个新的元素

  1. //将List<Entity> 转换成 List<Long>
  2. entityList.stream().map(Entity::getId).collect(Collectors.toList());

(6) sorted() 自然排序、 sorted(Comparator) 定制排序

  1. //自然排序,需要排序的对象实现了Comparator接口
  2. List<Integer> list = Arrays.asList(4, 8, 9, 10);
  3. list.stream().sorted().forEach(System.out::println);
  4. //定制排序,按照id从小到大排序,当返回值为1时,会交换两个元素的位置,为0和-1都不会交换位置
  5. entityList.stream().sorted((e1, e2) -> (int)(e2.getId() - e1.getId())).forEach(System.out::println);

3.2 终止流,以下操作使用后,流终止,得出结果

(1) allMatch() 检查流中所有元素是否满足条件,返回值时boolean

  1. //判断所有元素的id是否都大于20
  2. boolean allMatch = entityList.stream().allMatch(entity -> entity.getId() > 20);

(2) anyMatch() 检查流中是否有任意一个元素满足条件,返回值为boolean

  1. //检查是否至少有一个元素满足id大于20的条件
  2. boolean anyMath = entityList.stream().anyMatch(entity -> entity.getId > 20);

(3) noneMatch() 检查流中所有元素是否没有匹配指定条件,如果没有一个匹配成功,返回true,其实就是allMatch的取反操作

  1. //判断是否没有一个元素的id < 20
  2. boolean noneMatch = entityList.stream().noneMatch(entity -> entity.getId() < 20);

(4) findFirst() 返回第一个元素

  1. //返回流中的第一个元素
  2. Optional<Entity> first = entityList.stream().findFirst();

(5) findAny() 返回流中的任意一个元素

  1. //返回流中的任意一个元素
  2. Optional<Entity> any = entityList.stream().findAny();

(6) count() 返回流中元素的总个数

  1. //返回流中元素的总个数
  2. long count = entity.stream().count();

(7) max() 返回流中的最大值, min() 返回流中最小值

  1. //查找所有元素中id最大的元素
  2. Optional<Entity> maxIdEntity = entityList.stream().max((e1, e2) -> (int)(e1.getId() - e2.getId()));
  3. //查找所有元素中id最小的元素
  4. Optional<Entity> minIdEntity = entityList.stream().min((e1, e2) -> (int)(e1.getId() - e2.getId()));

(8) reduce() 规约操作可以将所有元素全部结合起来得到一个值

  1. Optional<Long> reduceSum = entityList.stream().map(Entity::getId).reduce(Long::sum);

(9) collect() 收集操作,将流转换成其它形式,接收一个Collector接口实现,用于收集stream所有元素的方法

  1. //返回一个List
  2. List<Entity> list = entityList.stream().filter(entity -> entity.getId() > 20).collect(Collectors.toList());
  3. //返回一个Set
  4. Set<Entity> set = entityList.stream().filter(entity -> entity.getId() > 20).collect(Collectors.toSet());
  5. //返回一个Map
  6. Map<Long, Entity> map = entityList.stream().filter(entity -> entity.getId() > 20).collect(Collectors.toMap(Entity::getId, e -> e));