zoukankan      html  css  js  c++  java
  • 【JDK8】 Optional 用法记录

    Optional

    of 与 ofNullable 的区别

       /**
       * Returns an {@code Optional} with the specified present non-null value.
       *
       * @param <T> the class of the value
       * @param value the value to be present, which must be non-null
       * @return an {@code Optional} with the value present
       * @throws NullPointerException if value is null
       */
      public static <T> Optional<T> of(T value) {
          return new Optional<>(value);
      }
    
        /**
         * Returns an {@code Optional} describing the specified value, if non-null,
         * otherwise returns an empty {@code Optional}.
         *
         * @param <T> the class of the value
         * @param value the possibly-null value to describe
         * @return an {@code Optional} with a present value if the specified value
         * is non-null, otherwise an empty {@code Optional}
         */
        public static <T> Optional<T> ofNullable(T value) {
            return value == null ? empty() : of(value);
        }
    
    • of 不允许为null
    • ofNullable 允许为null
  • 相关阅读:
    38
    37
    学记
    36.java_exception_test
    c++中enum的用法——枚举类型
    35
    34
    33
    32
    31
  • 原文地址:https://www.cnblogs.com/amberbar/p/13356445.html
Copyright © 2011-2022 走看看