Optional as a single-value container that either contains a value or doesn’t ( it is then said to be ‘empty’)
Optional.empty() // with non-null value
Optional.of(image) // if image were null, a NullPointerException would be thrown
Optional.ofNullable(image) // create an Optional object that may hold a null value
orElse() method, provides a default value if Optional is empty
Optional.empty() // with non-null value
Optional.of(image) // if image were null, a NullPointerException would be thrown
Optional.ofNullable(image) // create an Optional object that may hold a null value
orElse() method, provides a default value if Optional is empty
// Enum.valueOf with Optional
Optional.ofNullable(person.getGender()).ifPresent(type -> person.setGenderType(GenderType.valueOf(type)));
댓글
댓글 쓰기