[TIL] 23.04.16

JH4·2023년 4월 16일
0

TIL

목록 보기
11/11

오늘의 문제


stream 사용하지 않고 전체 게시글 조회하기

문제해결시도

stream을 향상된 for 문으로 바꿔주기

해결과정

List<PostResponseDto> postResponseDtos = new ArrayList<>();
	List<Post> allByOrderByCreatedAtDesc = postRepository.findAllByOrderByCreatedAtDesc();
        for (Post post : allByOrderByCreatedAtDesc) {
            postResponseDtos.add(new PostResponseDto(post));
        }
        return postResponseDtos;

비어있는 PostResponseDto List를 만들어주고
PostResponseDto를 postRepository.findAllByOrderByCreatedAtDesc(); 에서 받아온 데이터로 채워준다.

TIL : 내가 얻은 것

stream을 사용할 줄 몰라도 적절하게 for 문을 사용하면 완성할 수 있었다.
Spring을 하면서 자바를 꾸준하게 공부해야하는 이유가 더 확실해졌다.

profile
개발일지

0개의 댓글