Java

[Java] Lambda & Stream 2

quedevel 2020. 2. 26. 14:01
728x90
반응형

이번엔 Domain 만들어서 테스트

Board라는 Domain을 만들었다.

public class Board {

    private Long bno;
    private String writer;
    private String content;
    private LocalDate regdate;
    private LocalDate updatedDate;

    public static class Builder {

        private Long bno;
        private String writer;
        private String content;
        private LocalDate regdate;
        private LocalDate updatedDate;

        public Builder bno(Long bno) {
            this.bno = bno;
            return this;
        }

        public Builder writer(String writer) {
            this.writer = writer;
            return this;
        }

        public Builder content(String content) {
            this.content = content;
            return this;
        }

        public Builder regdate(LocalDate regdate) {
            this.regdate = regdate;
            return this;
        }

        public Builder updatedDate(LocalDate updatedDate) {
            this.updatedDate = updatedDate;
            return this;
        }

        public Board build() {
            return new Board(this);
        }
    }

    private Board(Builder builder) {
        this.bno = builder.bno;
        this.writer = builder.writer;
        this.content = builder.content;
        this.regdate = builder.regdate;
        this.updatedDate = builder.updatedDate;
    }

    public Long getBno() {
        return bno;
    }

    public String getWriter() {
        return writer;
    }

    public String getContent() {
        return content;
    }

    public LocalDate getRegdate() {
        return regdate;
    }

    public LocalDate getUpdatedDate() {
        return updatedDate;
    }

}

빌더 패턴을 이용해서 만들었는데 lombok 없으면 다신 안만들 예정이다....

맞게 만든지도 모르겠다...

 

도메인 클래스를 가지고 Lambda & Stream 테스트

public class Main {

    public static void main(String[] args) {

        List<Board> boardList = new ArrayList<>();

        IntStream.range(1, 10).forEach(i -> {
            boardList.add(new Board.Builder().bno(i + 0L).writer("writer..." + i % 3).content("content...")
                    .regdate(LocalDate.now()).updatedDate(LocalDate.now()).build());
        });

        // Lombok 없으니 드럽게 불편하군

        boardList.stream().filter(board -> board.getWriter().equals("writer....4")).collect(Collectors.toList())
                .forEach(System.out::println);

        System.out.println(boardList.stream().filter(board -> board.getWriter().equals("writer....4"))
                .collect(Collectors.counting()));

        List<String> upcaseTester = Arrays.asList("a1", "b1", "c1", "a1", "e1", "f1", "g1");

        upcaseTester.stream().map(String::toUpperCase).forEach(System.out::println);

        upcaseTester.stream().filter(str -> str.startsWith("a")).map(String::toUpperCase).forEach(System.out::println);

        String[][] sample = new String[][] { { "z", "x" }, { "c", "a" }, { "e", "z" }, { "z", "h" }, { "i", "j" } };

        System.out.println(Arrays.stream(sample).flatMap(arr -> Arrays.stream(arr)).sorted().collect(Collectors.toSet()));
        
	}
}

 

결과

0
A1
B1
C1
A1
E1
F1
G1
A1
A1
[a, c, e, h, x, i, j, z]

좀 더 간결하고 명시적인것 같다!!

728x90
반응형

'Java' 카테고리의 다른 글

[Java] 오버로딩 VS 오버라이드  (0) 2020.12.27
[Java] OOP의 특징  (0) 2020.12.27
[Java] 객체지향언어  (0) 2020.12.27
[Java] Lambda & Stream 3  (0) 2020.02.26
[Java] Lambda & Stream 1  (0) 2020.02.26