Stream流的具体细节可以查看如下链接:Java 8 Stream | 菜鸟教程
以下介绍几种我在实际开发中使用到他的场景:
1.集合中元素的去重操作:
虽然distinct()方法也可以进行去重,但是只能比较整个对象,不能比较对象里属性。
2.集合中元素的去重操作(根据集合中对象的具体的属性进行去重):
新建一个方法,利用HashMap的key不能重复的特性,进行对象去重
3.需求:给定集合(集合中为有多个属性的对象),获取对象中的某一字段从而构成新的集合,并进行去重操作
//我们将要过滤的集合
List<ThirdChannelWarehouseResVo> listBaseResponseData =
listThirdChannelWarehouseBaseResponse.getData();
List<Integer> warehouseIds = listBaseResponseData.stream().map(ThirdChannelWarehouseResVo::getWarehouseId).distinct().collect(Collectors.toList());
4.需求:List转换成Map时遇到重复主键问题
这样转换会报错,因为ID重复。
可以这样做
5.需求:现在有一个集合,对集合进行分组操作
//要操作的集合
List<BusinessStockVo> businessStockList = businessStockReqVo.getBusinessStockList();
//具体的操作
List<String> dupList = businessStockList.stream().collect(
Collectors.groupingBy(item->item.getShopId()+":"+item.getSku(), Collectors.counting())
).entrySet().stream().filter(e -> e.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());
通过上述操作,我们先以item.getShopId()+":"+item.getSku()作为key,value为统计key出现的次数,第二次是对出现1次以上的key进行了collect
PS:Collectors的具体用法参考如下:
java8中的Collectors.groupingBy用法_兴国First的博客-优快云博客_collectors.groupingby
6.List集合转换
怎么把List<List<User>>类型的集合转换List<User>类型
7.将Map中的key或者是value单独弄成一个集合
leyouOrderExistReqVo这个字段是一个map集合
List<String> takeOutThirdOrderList = leyouOrderExistReqVo.getOrderExistsList().entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());
通过上述的方式可以将map中的key字段,或者是value字段单独抽取出来成为一个新的集合
8.需求:根据集合中的实体中的具体的属性进行收集(先判断该字段非空)
takeOutRefundThirdOrderResVos是事先已经得到的一个集合
List<String> takeOutThirdNums = takeOutRefundThirdOrderResVos.stream().distinct().filter(e->null != e.getRefundThirdOrderNum()) .map(TakeOutRefundThirdOrderResVo::getRefundThirdOrderNum).collect(Collectors.toList());
9.需求:两个集合中的数据,存在赋值的情况,传统我们使用的是两层for循环的方式,利用stream流可以按下面的方式实现。
Pelple pelple3 = new Pelple();
pelple3.setName("李白");
pelple3.setHistory("诗仙");
pelple3.setUnit("青莲居士");
pelple3.setAge(new BigDecimal(30));
Pelple pelple1 = new Pelple();
pelple1.setName("李白");
pelple1.setHistory("诗仙");
pelple1.setUnit("青莲居士");
pelple1.setAge(new BigDecimal(20));
Pelple pelple2 = new Pelple();
pelple2.setName("杜甫");
pelple2.setHistory("诗仙");
pelple2.setUnit("青莲居士");
pelple2.setAge(new BigDecimal(20));
List<Pelple> peopleList = new ArrayList();
peopleList.add(pelple3);
peopleList.add(pelple1);
peopleList.add(pelple2);
System.out.println("操作前的人的集合"+" :"+peopleList);
Student student1 = new Student("李白",new BigDecimal(30));
Student student2 = new Student("杜甫",new BigDecimal(30));
List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
System.out.println("操作前的学生集合"+" :"+studentList);
List<Student> studentList1 = peopleList.stream().map(people -> {
return studentList.stream().filter(student -> { //条件判断
return people.getAge().equals(student.getAge());
}).filter(student -> people.getName().equals(student.getName())).map(student -> {
student.setAge(people.getAge());
return student; //返回的结果
}).collect(Collectors.toList());
}).flatMap(List::stream).collect(Collectors.toList());
以上是目前使用到的,后续还会更新