JDK8 Stream操作整理

1,forEach

this.quoteItemList.forEach(p -> p.setMode(mode));

2,获取对话属性,去重后生成集合

List<String> projects = this.quoteItemList.stream().map(p -> p.getVersion()).distinct().collect(Collectors.toList());

3,过滤后汇总

 double totalRealManDay = this.quoteItemList.stream().filter(p -> p.getAssignee().equals(person.getName())).mapToDouble(p -> p.getSpend()).sum() ;

sum可以改成count等其它函数

4,排序号取第一个

ProfitResult monthResult = calcResult.stream().sorted(Comparable::compareTo).filter(p -> p.getPeriod().equals(period)).findAny().get();
if (monthResult != null) {
}

5,分组统计转成Map

Map<String, Double> monthMap = this.data.stream().filter(p -> p.getVersion().equals(project)).collect(
    Collectors.groupingBy(QuoteItem::getFinishMonth, Collectors.summingDouble(QuoteItem::getEstimate))
);

6,分页获取

//取出一页数据的id列表
List<String> ids = list.stream().skip((findDoctorVo.getPage() -1) * findDoctorVo.getPagesize())
    .limit(findDoctorVo.getPagesize())
    .map(TeamVo::getTeamId)
    .collect(Collectors.toList());

 7,转换成其它对象

List<SessionListVo> newList = list.stream().map(SessionListVo::new).collect(Collectors.toList());

8,查询并拼接

String newTags = tags.stream().filter((p) -> !p.equals(this.defaultTag)).collect(Collectors.joining(this.SPLITER)); 

猜你喜欢

转载自www.cnblogs.com/season2009/p/10245651.html