优化以下代码 ListYxStoreCouponIssue couponIssueUsersLists = new ArrayList; ListYxStoreCouponIssue commonCouponList = new ArrayList; ListYxStoreCouponIssue productCouponList = new ArrayList;
List
// Build a set to store the IDs of the products to avoid duplicates
Set
// Iterate over the product IDs set
for (Long productId : productIdSet) {
// Get the list of coupon issues for the current product from the cache or the database
List
// Use a stream to remove duplicates from the list of coupon issues couponIssueUsersLists = couponIssueUsersLists.stream() .distinct() .collect(Collectors.toList());
// Build a wrapper to query the list of failed coupon users
LambdaQueryWrapper
// Use a stream to filter out the failed coupon issues from the list of coupon issues
couponIssueUsersLists = couponIssueUsersLists.stream()
.filter(couponIssue ->
couponUserService.count(new LambdaQueryWrapper
// Use a stream to sort the list of coupon issues by price couponIssueUsersLists = couponIssueUsersLists.stream() .sorted(Comparator.comparing(YxStoreCouponIssue::getCouponPrice).reversed()) .collect(Collectors.toList());
// Use a stream to divide the list of coupon issues into common and product coupon lists couponIssueUsersLists.forEach(couponIssue -> { if (couponIssue.getCtype().equals(CouponEnum.TYPE_0.getValue())) { commonCouponList.add(couponIssue); } else if (couponIssue.getCtype().equals(CouponEnum.TYPE_1.getValue())) { productCouponList.add(couponIssue); } });
// Build the result object CouponConfirmOrderVo couponConfirmOrderVo = new CouponConfirmOrderVo(); couponConfirmOrderVo.setCommonCouponList(commonCouponList); couponConfirmOrderVo.setProductCouponList(productCouponList); return couponConfirmOrderVo
原文地址: https://www.cveoy.top/t/topic/gpvX 著作权归作者所有。请勿转载和采集!