??????????? Java 8 ???????Щ??????? API?? ????????? Optional. ????????????????? ??????赭д?????????????????? NullPointException ?????? ??????????????д??
????Optional<User> user = ……
????if (user.isPresent()) {
????return user.getOrders();
????} else {
????return Collections.emptyList();
????}
??????????ò???????????????????????? ?????????????????? User ????????? ??????????д??
????User user = …..
????if (user != null) {
????return user.getOrders();
????} else {
????return Collections.emptyList();
????}
???????????????κη??. ??????????????????ú? Java 8 Optional ????????????.
????????????????? ???????????????????????? ????????????????????????????? ?????????С???????????????????. ????????????????????????????????????????? ???????????кβ???? ???仰??????????л??? Java 8 ?? Optional ??? ??????????????? null ??????????? ?????????μ?? ???????? Java 8 Optional ?????????.
??????????? ??????????????????????? Optional ??? ?????????????
???????? isPresent() ?????
???????? get() ?????
????Optional ?????????/????????
????Optional ????????????????
????isPresent() ?? obj != null ???κη?? ?????????????????????. ????? isPresent() ?????? get() ?????? IntelliJ IDEA ?л?????澯
????Reports calls to java.util.Optional.get() without first checking with a isPresent() call if a value is available. If the Optional does not contain a value?? get() will throw an exception. (???? Optional.get() ????????? isPresent() ??????????. ???? Optional ???????????? get() ????????????)
?????? Optional ???????????????????????? IntelliJ IDEA ?и?????????????
????Reports any uses of java.util.Optional<T>?? java.util.OptionalDouble?? java.util.OptionalInt?? java.util.OptionalLong or com.google.common.base.Optional as the type for a field or a parameter. Optional was designed to provide a limited mechanism for library method return types where there needed to be a clear way to represent “no result”. Using a field with type java.util.Optional is also problematic if the class needs to be Serializable?? which java.util.Optional is not. (????κ??? Optional ???????????λ?????????????????. Optional ????????????? ???????????????????μ????????. Optional ???????????л??? ??????????????????)
???????? Optional ?????????????????????????? isPresent() ?? get() ??????????:
????public<U> Optional<U> map(Function<? super T?? ? extends U> mapper)
????public T orElse(T other)
????public T orElseGet(Supplier<? extends T> other)
????public void ifPresent(Consumer<? super T> consumer)
????public Optional<T> filter(Predicate<? super T> predicate)
????public<U> Optional<U> flatMap(Function<? super T?? Optional<U>> mapper)
????public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
????????????????????????????????????????????????.
??????????ò?????? Optional ??????????: Optional.of(obj)??  Optional.ofNullable(obj) ??????? Optional.empty()
????Optional.of(obj): ???????? obj ?????? null ???? ???????????????????? NullPointerException ??????.
????Optional.ofNullable(obj): ????????????? ???????????????? Optional ???. ???????? ?? null ??????? Optional.empty()?? ?? null ???? Optional.of(obj).
??????????????????? Optional.ofNullable(obj) ???????? ??????????????????? Optional ?????????? ???δ??? ???? Optional.of(obj) ?α????????? ???????