stream

pepe·2025년 11월 6일


input-> swimmers list
filter 로 거르고
map으로 매핑
collect -> 종단 함수 (string, map형태로 변환)

IntStream.rangeClosed(1, 10) // 1~10
                .forEach(
                        new IntConsumer() {
                            @Override
                            public void accept(int value) {
                                System.out.println(value);
                            }
                        }
                );
//람다
IntStream.rangeClosed(1, 10) // 1~10
                .forEach(
                        (value) -> {
                            System.out.println(value);
                        }
                );
//람다
IntStream.rangeClosed(1, 10) // 1~10
                .forEach(System.out::println);

stream은 원본이 바뀌지않는다.
type이 바뀔때는 map이 아니라mapToXXX를 써야한다.

Optional

있거나 없거나
Optional.orElse(null) -> 있으면 값 반환 없으면 null 반환

Optional<Person> opPerson = people.stream()
                .filter(e -> e.getId() == 2)
                .findFirst();

Person found = opPerson.orElse(null);

if (found == null) {
	System.out.println("not found");
	return;
}

System.out.println("found : " + found.getName());
profile
pepe

0개의 댓글