目录
2.4.1、filter()方法里编写过滤条件,符合条件的添加到新的集合里
2.4.3、findFirst() 查找满足条件的第一条数据
1、背景及出现原因
Java8之前是无法将方法赋值给一个变量。Java8的一个重大特性就是引入了函数式编程,变量可以被赋值为方法的参数和返回值,这就叫做函数式编程!
2、功能及实现
2.1、组成新的集合
List<TagStyleOutputDTO> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
TagStyleOutputDTO dto = new TagStyleOutputDTO();
dto.setSeqNo(i*3);
list.add(dto);
}
//1-1:获取对象属性集合
List<Integer> tagIdList
= list.stream().map(item->item.getSeqNo()).collect(Collectors.toList());
//1-2:获取新的集合
List<DataTagOutputDTO> dataTagList = list.stream().map(item->{
DataTagOutputDTO newDto = new DataTagOutputDTO();
BeanMapper.map(item,newDto);
return newDto;
}).collect(Collectors.toList());
2.2、排序
2.2.1、升序(默认)
list.sort(Comparator.comparing(TagStyleOutputDTO::getSeqNo));
2.2.2、降序
list.sort(Comparator.comparing(TagStyleOutputDTO::getSeqNo).reversed());
#新List接收排序结果
List newlist =
list.stream().sorted(
Comparator.comparing(TagStyleOutputDTO::getSeqNo).reversed()
).collect(Collectors.toList())
2.2.3、多字段排序
// 先按a倒序排列、再按b倒序排列、最后按c正序排列
list.sort(Comparator.comparing(SortTest::getA, Comparator.reverseOrder())
.thenComparing(SortTest::getB, Comparator.reverseOrder())
.thenComparing(SortTest::getC));
2.3、集合转Map
2.3.1、不重复key,如果有重复key会报错
# Function.identity代表对象本身
# toMap第一个参数为key,第二个参数为value
Map<Integer,TagStyleOutputDTO> seqMap =
list.stream().collect(Collectors.toMap(TagStyleOutputDTO::getSeqNo,item->item));
或者
Map<Integer,TagStyleOutputDTO> seqMap2 =
list.stream().collect(Collectors.toMap(TagStyleOutputDTO::getSeqNo, Function.identity()));
2.3.2、重复key 导向key2则代表后者覆盖前者
Map<Integer,TagStyleOutputDTO> seqMap3 =
list.stream().collect(
Collectors.toMap(TagStyleOutputDTO::getSeqNo,item->item,(k1,k2)->k2));
2.3.3、分组 相同key分到相同组
#key为对象固有字段
Map<Integer,List<TagStyleOutputDTO>> seqMap4 =
list.stream().collect(Collectors.groupingBy(TagStyleOutputDTO::getSeqNo));
#key自定义转换
Map<String,List<TagStyleOutputDTO>> seqMap4 =
list.stream().collect(Collectors.groupingBy(item->item.getSeqNo+"Luosy"));
2.3.4、key和value为基本数据类型
#item里tagId不能为null,否则报错NPE
Map<Integer,Long> seqMap =
list.stream().collect(Collectors.toMap(TagStyleOutputDTO::getSeqNo,item->item.getTagId));
2.4、过滤
2.4.1、filter()方法里编写过滤条件,符合条件的添加到新的集合里
List<TagStyleOutputDTO> seqMap5 =
list.stream().filter(item->item.getSeqNo() > 6).collect(Collectors.toList());
2.4.2、findAny() 查找满足条件的任意一条数据
# Optional用来返回包装结果,是在java.util包下的一个用于代替null的一个工具类
# Optional类是一个可以为null的容器对象,如果值存在则isPresent()返回true,get()方法返回对象
# stream串行流:返回第一条数据作为任意一条数据
# parallelStream并行流:多个线程来并发处理,适用于集合size较大的情况效率更快,返回严格意义上的任意数据
//串行流
Optional<TagStyleOutputDTO> findDto
= list.stream().filter(item->item.getSeqNo() >2).findAny();
System.out.println("***findDto1:"+JSON.toJSONString(findDto.isPresent()));
System.out.println("***findDto2:"+JSON.toJSONString(findDto.get().getSeqNo()));
//并行流
Optional<TagStyleOutputDTO> findDto
= list.parallelStream().filter(item->item.getSeqNo() >2).findAny();
System.out.println("***findDto1:"+JSON.toJSONString(findDto.isPresent()));
System.out.println("***findDto2:"+JSON.toJSONString(findDto.get().getSeqNo()));
2.4.3、findFirst() 查找满足条件的第一条数据
Optional<TagStyleOutputDTO> findFirstDto
= list.stream().filter(item->item.getSeqNo() > 2).findFirst();
System.out.println("findFirstDto1:"+JSON.toJSONString(findFirstDto.isPresent()));
System.out.println("findFirstDto2:"+JSON.toJSONString(findFirstDto.get().getSeqNo()));
2.4.4、过滤后再返回属性集合
List<TbPlanItemLabel> piLabelList = new ArrayList<>();
List<String> selfLabelIdList
= piLabelList.stream().filter(piItem->piItem.getLabelType().equals(0))
.map(TbPlanItemLabel::getLabelId).collect(Collectors.toList());
2.5、遍历forEach
list.stream().forEach(item->{
item.setDefaultFlag(96376L);
});
2.6、累加reduce(x,y)
第一个参数是给出的初值,第二个参数是累加器
List<AccountTransExtOutBO> outpList = new ArrayList<>();
#存在空指针异常
BigDecimal outpRechargeAmount
= outpList.stream().map(
AccountTransExtOutBO::getRechargeAmount
).reduce(BigDecimal.ZERO,BigDecimal::add);
#处理空指针
BigDecimal outpRechargeAmount
= outpList.stream().map(
item->item.getRechargeAmount()==null?BigDecimal.ZERO:item.getRechargeAmount()
).reduce(BigDecimal.ZERO,BigDecimal::add);
2.7、累加sum
可能空指针异常
int returnTotalNum = returnOrderItemVoList.stream().mapToInt(ReturnOrderItemVo::getNum).sum();
防空指针异常
int returnTotalNum
= returnOrderItemVoList.stream().mapToInt(item->item.getNum()==null?0:item.getNum()).sum();