엄지월드

arr stream return 값 만들기 본문

java/Spring

arr stream return 값 만들기

킨글 2023. 7. 17. 22:32

 

 

import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.stream.Collectors;

@Getter
@Setter
class Board {
	private Long bno;
    private String title;
    private String content;
    private String writer_id;
    private String writer_name;
}

public Object getStream() {
    List<Board> boardList = new ArrayList<>();
    
    for (int i = 0; i < 3; i++) {
        Board board = new Board();
        board.setBno((long) (i+1));
        board.setTitle("title_"+i);
        board.setContent("content_"+i);
        board.setWriter_id("id_"+i);
        board.setWriter_name("name_"+i);

        boardList.add(board);
    }
    
    return boardList.stream()
        .map(userPoint -> objectMapper.convertValue(userPoint, Map.class))
        .collect(Collectors.toList());
}

 

[{"bno":1,"title":"title_0","content":"content_0","writer_id":"id_0","writer_name":"name_0"},{"bno":2,"title":"title_1","content":"content_1","writer_id":"id_1","writer_name":"name_1"},{"bno":3,"title":"title_2","content":"content_2","writer_id":"id_2","writer_name":"name_2"}]
Comments