首页 > 编程语言 >Java8lambda表达式的使用

Java8lambda表达式的使用

时间:2022-08-31 09:46:13浏览次数:65  
标签:toMap group stream Collectors collect 使用 Java8lambda 表达式

Java8提供很多lambda表达式,能够使得开发代码更加简洁优美。本文学习lambda后总结了一些lambda表达式的使用。

1、list.forEach

this.supplierQuoteByIdRespose.getQuoteInfo().stream().forEach(quoteItemResp -> {
List<SupplierQuoteItemInfoResp> itemGroup = quoteItemResp.getItemGroup();
itemGroup.stream().forEach(group -> {
QuoteItemDto itemDto = this.quoteItemList.stream().filter(quoteItemDto -> quoteItemDto.getEnquiryItemId()
      .equals(group.getId())).findAny().orElse(null);
if (itemDto != null) {
//标记已采纳
group.setAdopt(true);
this.adopt=true;
}
});
});

2、Collectors.toMap,在很多时候我们需要将对象列表转为Map,这时候使用Collectors.toMap就会非常方便

Map<Long, TranslationInfo> mapTranslationInfo = listTranslationInfo.stream()
.collect(Collectors.toMap(TranslationInfo::getId, Function.identity(), (key1, key2) -> key2));

3、list过滤符合条件的对象

ArrayList<TranslationInfo> collect = translations.stream()
                .filter(y -> Objects.equals(y.getRearEndStatus(), TranslationRearEndStatusEnum.NotEnquiry.getName()))
                .collect(Collectors.toCollection(ArrayList::new));

4、取对象list中某个参数列表

List<String> pIds = items.stream().map(QueryDetailProductResp::getPid).collect(Collectors.toList());

5、过滤对象列表取对象中的元素列表

List<Long> pidList = translationInfoList.stream().filter(TranslationInfoDto ->
                Objects.equals(saleCategoryPathForTire, TranslationInfoDto.getCategoryPath()))
                .map(TranslationInfoDto::getId).collect(Collectors.toList());

 

标签:toMap,group,stream,Collectors,collect,使用,Java8lambda,表达式
From: https://www.cnblogs.com/Mrshu-a-progranmer/p/16641888.html

相关文章