collect(Collectors.toList()) vs Stream.toList()

왕감자·2025년 2월 19일

2025 내일배움캠프

목록 보기
17/39
public List<ReviewResponseDto> getReviewList(String shop_id) {
        Shop shop = shopRepository.findById(UUID.fromString(shop_id))
                .orElseThrow(() -> new CustomException(ReviewErrorCode.SHOP_NOT_FOUND));

        List<Review> reviews = reviewRepository.findByShop(shop);

        return reviews.stream()
                .map(ReviewResponseDto::of)
                .collect(Collectors.toList());
}
public List<ReviewResponseDto> getReviewList(String shop_id) {
    Shop shop = shopRepository.findById(UUID.fromString(shop_id))
            .orElseThrow(() -> new CustomException(ReviewErrorCode.SHOP_NOT_FOUND));

    List<Review> reviews = reviewRepository.findByShop(shop);

    return reviews.stream()
            .map(ReviewResponseDto::of)
            .toList();  // collect 대신 toList() 사용
}

0개의 댓글