/** * If a value is present, invoke the specified consumer with the value, * otherwise do nothing. * * @param consumer block to be executed if a value is present * @throws NullPointerException if value is present and {@code consumer} is * null */ publicvoidifPresent(Consumer<? super T> consumer){ if (value != null) consumer.accept(value); }
/** * Return the value if present, otherwise return {@code other}. * * @param other the value to be returned if there is no value present, may * be null * @return the value, if present, otherwise {@code other} */ public T orElse(T other){ return value != null ? value : other; }
/** * Return the value if present, otherwise invoke {@code other} and return * the result of that invocation. * * @param other a {@code Supplier} whose result is returned if no value * is present * @return the value if present otherwise the result of {@code other.get()} * @throws NullPointerException if value is not present and {@code other} is * null */ public T orElseGet(Supplier<? extends T> other){ return value != null ? value : other.get(); }
/** * If a value is present, apply the provided mapping function to it, * and if the result is non-null, return an {@code Optional} describing the * result. Otherwise return an empty {@code Optional}. * * @apiNote This method supports post-processing on optional values, without * the need to explicitly check for a return status. For example, the * following code traverses a stream of file names, selects one that has * not yet been processed, and then opens that file, returning an * {@code Optional<FileInputStream>}: * * <pre>{@code * Optional<FileInputStream> fis = * names.stream().filter(name -> !isProcessedYet(name)) * .findFirst() * .map(name -> new FileInputStream(name)); * }</pre> * * Here, {@code findFirst} returns an {@code Optional<String>}, and then * {@code map} returns an {@code Optional<FileInputStream>} for the desired * file if one exists. * * @param <U> The type of the result of the mapping function * @param mapper a mapping function to apply to the value, if present * @return an {@code Optional} describing the result of applying a mapping * function to the value of this {@code Optional}, if a value is present, * otherwise an empty {@code Optional} * @throws NullPointerException if the mapping function is null */ public<U> Optional<U> map(Function<? super T, ? extends U> mapper){ Objects.requireNonNull(mapper); if (!isPresent()) return empty(); else { return Optional.ofNullable(mapper.apply(value)); } }
map 方法接收一个 Function 对象。返回一个新的 Optional 对象。
当前 Optiaon 对象包含的值为 null 时,返回一个包含 null 对象的 Optional 对象;当前 Optional 包含的值 value 不为 null 时,通过在 value 上应用 Function 对象的 apply 方法得到新值 value1 并构造一个包含 value1 的新 Optional 对象。即该方法可以改变值。
@Override public String toString(){ return"Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } }
filter 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** * If a value is present, and the value matches the given predicate, * return an {@code Optional} describing the value, otherwise return an * empty {@code Optional}. * * @param predicate a predicate to apply to the value, if present * @return an {@code Optional} describing the value of this {@code Optional} * if a value is present and the value matches the given predicate, * otherwise an empty {@code Optional} * @throws NullPointerException if the predicate is null */ public Optional<T> filter(Predicate<? super T> predicate){ Objects.requireNonNull(predicate); if (!isPresent()) returnthis; else return predicate.test(value) ? this : empty(); }
该方法传入一个 Predicate 对象,当前 Optiaonal 包含的值 value 为 null 或者在 value 上应用 Predicate 对象的 test 方法返回 false 时,该方法返回空的 Predicate 否则返回自身。