1. 程式人生 > >JDK8 Stream操作整理

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));