null 안정성을 제공하기 때문에 사용
null 이 나올 수 있다고 알려주는 방법
메서드 사용자는 Optional 타입인 것을 확인하면 null 처리 하면 됨
- Optional.of(value) 절대 null이 아니어야 함
- Optional.ofNullable(value) null일 수도 있음
- Optional.empty() 빈 Optional 생성
- get() 값 꺼냄 (값이 없으면 예외 발생)
- orElse(default) 값이 없으면 기본값 리턴
- orElseGet(Supplier) 값이 없으면 람다 결과 리턴
- orElseThrow() 값이 없으면 예외 발생
- isPresent() 값이 있는지 확인
- ifPresent(Consumer) 값이 있으면 처리
- filter(Predicate) 조건에 맞는 값만 유지
of(), ofNullable()
@Test
public void t1() {
String name = "metacoding";
// 1. null 안정성을 제공함. xx.get() -> xx 가 null 이면 nullpointException 이 터진다
// Optional -> Promise 같은 선물박스다
Optional<String> opt = Optional.of(name); // <- of : 넣을 때 에러남. of 에서 터짐
System.out.println(opt.get());
Optional<String> opt2 = Optional.ofNullable(name); // ofNullable -> 넣을 때 에러가 안남
if (opt2.isPresent()) { // 존재하는지 확인 가능
System.out.println(opt2.get()); // <- get() 에서 터짐
} else {
System.out.println("선물박스에 값이 없어요");
}
}
Optional.of
→ 넣을 때 null 이면 예외처리 됨
Optional.ofNullable
→ 넣은 후에 get() 할 때 예외처리 됨
get
→ 옵셔널 클래스에 들어간 객체를 바로 꺼냄
isPresent
→ 옵셔널 클래스에 객체가 null 이면 false, 아니면 true 를 반환
orElseThrow()
@Test
public void t2() {
String name = null;
Optional<String> opt = Optional.ofNullable(name);
String result = opt.orElseThrow(() -> new RuntimeException("값이 없습니다")); // 값이 있으면 넣고, 없으면 throw 한다
System.out.println(result);
}
orElseThrow
→ 값이 없으면 throw 를 터트림
orElseGet()
@Test
public void t3() {
String name = null;
Optional<String> opt = Optional.ofNullable(name);
String result = opt.orElseGet(() -> "metacoding"); // 값이 없으면 기본값을 준다
System.out.println(result);
}
orElseGet
→ 값이 없으면 기본값을 넣어줌
Share article