Home > database >  how can i make this stream code as for-loop in java?
how can i make this stream code as for-loop in java?

Time:11-13

guys. I was doing my project for now, and then making service layers for this codes of reading select query, i have a question now.

i am pretty new web developer, so I am kinda confused of JAVA8 especially about lambda and stream.

//this code is on service layer to read db.
public List<CouponResDto.CouponInfo> getValidCoupon(OrderInDto.Search condition) {
        return toList(repository.findAll(condition.getCondition()))
                .stream()
                .map(this::toCouponInfo)
                .collect(Collectors.toList());

//to respond as Dto not entity.
public CouponResDto.CouponInfo toCouponInfo(Coupon coupon) {
        return new CouponResDto.CouponInfo()
                .setName(coupon.getName())
                .setAmount(coupon.getAmount())
                .setDescription(coupon.getDescription());
    }
    @Data
    @Accessors(chain = true)
    @NoArgsConstructor
    public static class CouponInfo {
        Long id;
        String name;
        int amount;
        String description;
    }

I wanna know how the "getValidCoupon" works, and make the code using for-loop, not stream. Can i get this logic?

CodePudding user response:

With for loop the code would look like this:

public List<CouponResDto.CouponInfo> getValidCoupon(OrderInDto.Search condition) {
    List<CouponResDto.CouponInfo> result = new ArrayList<>();
    for (var item : toList(repository.findAll(condition.getCondition()))) {
        result.add(toCouponInfo(item));
    }
    return result;
}

CodePudding user response:

Rather than rewriting it as a for-loop, here is an inline description of each step of the Stream implementation.

public List<CouponResDto.CouponInfo> getValidCoupon(OrderInDto.Search condition) {
  // Capturing result in local variable to be returned afterward just to make documentation easier to follow
  List<CouponResDto.CouponInfo> result =
        /**
         * execute the findAll query on the repository and pass the results
         * to our toList(..) method to produce a List<Coupon>
         */
        toList(repository.findAll(condition.getCondition())) // <-- List<Coupon>
        /**
         * invoke the List.stream() to stream the contents of that List
         */
        .stream() // <--------------------------------------------- Stream<Coupon>
        /**
         * Invoke the Stream's map(..) method to convert each Coupon to a
         * CouponResDto.CouponInfo using our toCouponInfo(..) method
         */
        .map(this::toCouponInfo) // <------------------------------ Stream<CouponResDto.CouponInfo>
        /**
         * Invoke the Stream's collect(..) the CouponResDto.CouponInfo instances
         * into a new List
         */
        .collect(Collectors.toList()); // <------------------------ List<CouponResDto.CouponInfo>
  // Return the List
  return result;
}
  •  Tags:  
  • java
  • Related