[Java] stream.map.collect를 이용한 List Mapping

Walter Mitty·2023년 6월 26일
0

면저 리스트로 만들 객체와 객체의 변수들

EduListDTO

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EduListDTO {

    private Integer eduContentId;

    private String eduTitle;

    private String type;

    private Integer percentage; // 진행 상황 (카드: 100%-이수, 100%미만-미이수, 동영상: percentage%/100%)
}

EduApplicationService

    @Transactional(readOnly = true)
    public EduListResponse findEduList(Integer userId) {
        List<EduContents> eduContents = eduContentsRepository.findAll();

        // EduContents 리스트를 EduListDTO에 매핑해서 List로 바꿔주기.
        List<EduListDTO> eduDtoList = eduContents.stream()
            .map(m-> new EduListDTO(m.getId(), m.getTitle(), m.getType(), m.getPercent)) // 여기 부분을 잘 맞춰주면 알아서 매핑해준다.
            .collect(Collectors.toList());

        List<EduListDTO> eduDtoList = eduCustomRepository.findEduAllList(userId);

        return EduListResponse.builder().eduListDTO(eduDtoList).build();
    }

0개의 댓글