본문 바로가기
카테고리 없음

Optional

by 공부 안하고 싶은 사람 2021. 2. 19.
반응형

빈 값
Optional optHouse = Optional.empty();

 

not null
Optional optHouse = Optional.of(new House());

 

flatMap 으로 연결

person.flatMap(Person::getHouse)
.flatMap(House::getInsurance)
.map(Insurance::getName).orElse("DolphaGo");

-> Optional house와insurance + name있다면 실행 없다면 orElse실행

 

isPresent() : null이면 false, 아니면 true

ifPresent(Consumer<T> block) : 있다면 인자 실행

ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) : 있다면 conumer실행, 없으면 runnable 실행

T get() : 있으면 반환, 없으면 NoSuchElementException 발생

T orElseGet(Supplier<T> other) : 있으면 반환, 없으면 other실행

T orElse(Supplier<T> other) : 있으면 반환 other실행, 없으면 other실행

orElseThrow(Supplier<? extends X> exceptionSupplier) : 있으면 반환, 없으면 예외 발생

 

728x90
반응형

댓글