Java 8 Optionalc��?��a?�e�??z?

e?�a??a?�a??Javac?�a??a�?i??a��a��a??e??e????�e?�?��cs�c�?aZ�i?se�?c�?a?�a???�???�a?�a?�a?�e?�a�za�?a??a??e??c�??Z?a��e?�a�za�?a??a??a?�?��aZ�e�?c�?a?�cs�?�???�a��??�a��e��a�?e�?a?�?��e?�a??e?�a�za�????a?�a??nulli??a????�a??e?zc�?cs�a�????a?�?�?e??a��a��a??a??a��a��?�???�cs�a?�?��a��e?�?�????a?�a?�c��a??Guavacs�a��e??APIe?�a�?e�?a�?cs�e��e??a��a?�a?�JVMc?�c?�e?�e?�??�a��Scalaa�?Ceylonc��a�?c�?a��a??a???�?a??APIa?�e�?a�?a?�e?�a??e��e??a��a????�cs�a�?a?�c?�?��c��a?�i??a��c�?a?�Scala???a��a?�e�?a�?a?�e?�a??e��e??a��

?��c�???�cs�Javai????�a��Java 8a?�a�?a?�a?�a???��cs�Optionalc��a��Optionalc��cs�Javadoc???e?�a��a?�i?s

e?�???a?�a??a??a�?a??nullcs�a�?a�?a??e�?a��a��?z?a�?a�?a??a?�isPresent()?�???�a?se?�a�ztruei??e�?c�?get()?�???�a?se?�a�ze??a??e�?a��

??�?��a?se�?a???Z?e�?Optionalc��a?�a?�cs�?�???�i??a?�e�se?�a?�a?�a??c�?a?�a��c�?a��a?�a??c�?a��

of

a??e?znullcs�a�?a?�a�?a?�a??Optionala��

of?�???�e�se?�a�?aZ�?�???�a?�a�?Optionalc��a��e?�e�?????�?cs�???i??a?�a�?a??e�??��a?�a�?cs�a?�?��a??e??a??nulla��a��?z?a?�a�?a?�?��a??nulli??a?�?S�a�?NullPointerException a��

//e�?c�?a�?aZ�?�???�a?�a�?Optionala�za?�
Optional<String> name = Optional.of("Sanaulla");
//a?�a�?a?�?��a??nulli???S�a�?NullPointerException.
Optional<String> someNull = Optional.of(null);

ofNullable

a????�a�scs�a�?a?�a�?a?�a??Optionali??a��?z???�a�scs�a�?a??nulli??a?�e?�a�za?�a??c�?cs�Optionala��

ofNullablea?Zof?�???�c�?a??i??a�?a?�cs�a??a?�???a??a�??Z?a?�a?�?��a??nullcs�??�a��a��c�?a?�a��a?�i?s

//a?�e??a?�a�?a?�a?�a??a??a?�a?�a��a?�a�?cs�Optionala�za?�
//a?�a��i??a�?a??'null'
Optional empty = Optional.ofNullable(null);

isPresent

e?za??a�???�c?�e�?

a��?z?a�?a�?a??e?�a�ztruei??a?�a?�e?�a�zfalsea��

c��a??a?�e??cs�a�?c�?i?s

//isPresent?�???�c�??????�?Y?Optionala�za?�a?�???a?�a?�a?�a�?
if (name.isPresent()) {
  //a??Optionala�za?�a��e�?c�?get()e?�a�za�?a�?a??cs�a�?
  System.out.println(name.get());//e?�a�?Sanaulla
}

get

a��?z?Optional??�a�?a?�a��a��e?�a�zi??a?�a?�?S�a�?NoSuchElementExceptiona��

a?Se??cs�c�?a?�a?�i??get?�???�c�????a?�a?�Optionala�za?�a?�cs�a�?a��a?�e????�a��c?�a?�a???S�a�?NoSuchElementExceptioncs�a?�a�?i?s

//?��e??a?�e??cs�a�?c�?a?se?�a�?i?sNo value present 
try {
  //a??c�?cs�Optionala�za?�a?Se�?c�?get()i???S�a�?NoSuchElementException
  System.out.println(empty.get());
} catch (NoSuchElementException ex) {
  System.out.println(ex.getMessage());
}

ifPresent

a��?z?Optionala�za?�??�a�?a?�a??a��e�?c�?consumeri??a?�a?�a??a?sa��c?�

e�?c?�e�?ifPresent?�???�i??e��a�?e?�e�?a?�e�?Consumerc��a��c��c��a?�e??i??Consumerc��a?�a?�a?�a???S?e�??�???�a��e???S?e�??�???�a??a?�a�?cs�a�?e?�e??a��c?�i??a?�?????�e?�a�za�?a��Java8?�????a??c�??Z?a??c�??Z?e�se?�lambdae??e??a??a?�a�?a?�?��a��

a��?z?Optionala�za?�??�a�?i??e�?c�?ifPresent()a??a�??Z?a?�?Z?a???��??�lambdae??e??a??a��c��a??a?�e??cs�a�?c�?i?s

//ifPresent?�???�?Z?a?�lambdae??e??a??a??a??a?�?��a��
//lambdae??e??a??a??Optionalcs�a�?e�?c�?consumere?�e??a��c?�a��
name.ifPresent((value) -> {
  System.out.println("The length of the value is: " + value.length());
});

orElse

a��?z???�a�?a?�a��a��e?�a�zi??a?�a?�e?�a�z??�a�scs�a��a�?a�?a��

a��?z?Optionala�za?�??�a�?a?�a��a��e?�a�zi??a?�a?�e?�a�zorElse?�???�a?�a�?cs�a?�?��a��c�?a?�a��a?�i?s

//a��?z?a�?a??a??nulli??orElse?�???�e?�a�zOptionala�za?�cs�a�?a��
//a��?z?a??nulli??e?�a�za?�a�?cs�?�????a��
//e?�a�?i?sThere is no value present!
System.out.println(empty.orElse("There is no value present!"));
//e?�a�?i?sSanaulla
System.out.println(name.orElse("There is some value!"));

orElseGet buy clomid online cheap, clomid without prescription.

orElseGeta?ZorElse?�???�c��a??i??a??a?�a??a?Za?�a?�cs�e�?e��a�?a��orElse?�???�a��a?�a�?cs�a��c��a??a??a??e�?e��a�?i??orElseGet?�???�a??a�??Z?a?�Supplier?Z?a??cs�a�zcZ�c�????c�Y???e�?e��a�?a��c�?a?�a��a?�i?s

//orElseGeta?ZorElse?�???�c��a??i??a??a?�a??a?ZorElsea?�a�?cs�???e�?e��a�?i??
//orElseGeta??a�??Z?a?�a?�a??lambdae??e??a??c�Y???e�?e��a�?a��
//e?�a�?i?sDefault Value
System.out.println(empty.orElseGet(() -> "Default Value"));
//e?�a�?i?sSanaulla
System.out.println(name.orElseGet(() -> "Default Value"));

orElseThrow

a��?z???�a�?a?�a��a��e?�a�zi??a?�a?�?S�a�?supplier?Z?a??a?�a�?cs�a?�a??a��

a??orElseGet?�???�a?�i????�a��a?�a�?a?�a??Supplier?Z?a??a��c��e�?i??a??orElseThrowa?�??�a��a??a�?a?�a�?a?�a??lambdae??e??a????�?�???�i??a��?z?a�?a??a�?a??????S�a�?a?�a??a��c�?a?�a��a?�i?s

try {
  //orElseThrowa?ZorElse?�???�c��a??a��a?Ze?�a�ze�?e��a�?a??a??i??
  //orElseThrowa?s?S�a�?lambdae??e??a????�?�???�c�Y???cs�a?�a?? 

  empty.orElseThrow(ValueAbsentException::new);
} catch (Throwable ex) {
  //e?�a�?: No value present in the Optional instance
  System.out.println(ex.getMessage());
}

ValueAbsentExceptiona�sa?�a��a?�i?s

class ValueAbsentException extends Throwable {

  public ValueAbsentException() {
    super();
  }

  public ValueAbsentException(String msg) {
    super(msg);
  }

  @Override
  public String getMessage() {
    return "No value present in the Optional instance";
  }
}

map

map?�???�?��???e????Za��a?�i?s

a��?z???�a�?i??a?�a??a��?��e??e�?c�?mappinga�??��a?�a?�e?�a�za�?a��a��?z?e?�a�za�?a??a??nulli??a?�a?�a�?a?�a?�mappinge?�a�za�?cs�Optionala??a??map?�???�e?�a�za�?i??a?�a?�e?�a�zc�?Optionala��

map?�???�c�????a??Optionala�za?�cs�a�??��e??a?�c?�a?�?�?a??a��e�se?�a?�c��a�zcZ�a?�Function?Z?a??cs�lambdae??e??a??a?�a�??�?a??a��a��?z?a?�a??c�Y?��Function?Z?a??i??a??a�?a?�e�???�cs�e?�c?�a?sa�?a��map?�???�c�?a?�a��a?�i?s

//map?�???�?��e??a?�a�?cs�lambdae??e??a??a?�?��a??Optionala�za?�cs�a�?e?�e??a?�?�?a��
//a??lambdae??e??a??cs�e?�a�za�?a?�a�??��cs�Optionala�za?�a??a??map?�???�cs�e?�a�za�?a��
Optional<String> upperName = name.map((value) -> value.toUpperCase());
System.out.println(upperName.orElse("No value found"));

flatMap

a��?z???�a�?i??a??a��?��e??mappinga�??��e?�a�zOptionalc��az�e?�a�za�?i??a?�a?�e?�a�zc�?Optionala��flatMapa?Zmapi??Funtioni?�?�???�c��a??i??a??a?�a??a?ZflatMapa?�cs�mappere?�a�za�?a?�e?�???Optionala��e�?c�?c��??Y?��i??flatMapa??a?sa??c��?z?c�?Optionala�?e?�a��

flatMap?�???�a?Zmap?�???�c��a??i??a??a?�a??a?Zmappinga�??��cs�e?�a�za�?a??a??a��map?�???�cs�mappinga�??��e?�a�za�?a??a�????a��a?�c��az�Ti??e�?flatMap?�???�cs�mappinga�??��a?�e?�???Optionala��

a?�c��mapa�??��i??a??c�?flatMape�?a��cs�c�?a?�a��a?�i?s

//flatMapa?Zmapi??Functioni?�e?za??c��a??i??a??a?�a??a?Za?�a�??�???�cs�lambdae??e??a??cs�e?�a�zc��az�a��
//map?�???�a?�cs�lambdae??e??a??e?�a�za�?a??a�????a��?�?c��az�i??a??mapa�??��e?�a�za?�a�?a?sa?�e?�a??Optionala�� 
//a?�flatMap?�???�a?�cs�lambdae??e??a??e?�a�za�?a?�e?�???Optionla�za?�a�� 
upperName = name.flatMap((value) -> Optional.of(value.toUpperCase()));
System.out.println(upperName.orElse("No value found"));//e?�a�?SANAULLA

filter

filtera???�???�e�se?�a?�a�?e�?a�s???a��a??Optionala�za?�cs�a�?e?�e??e?�?��a��?��??????e?�a��a?�i?s

a��?z???�a�?a?�a?�?�?e�??��e?�???a��e?�a�za?�a?�e??a�?cs�Optionali??a?�a?�e?�a�zc�?Optionala��

e?�a?�e?�e�?i??a??e??a?�a�?c�?cY?e?�a��a?�a??filter?�???�a?�a�?a?�?��a�?c�?a��???cs�i??e?�e�?a??a�?a?�a�?a?�a??lambdae??e??a??a��a??a?Zfiltera�??��??�a��a?�e??a?�a�?a�zcZ�a?�Predicate?Z?a??cs�lambdae??e??a??a��a��?z?a?�a??c�Y?��Predicate?Z?a??i??a??a�?a?�e�?e?�c?�?��c��a��

cZ�a????�???c?�c?�filtercs�a?�c�?c�???�i??a?�e??cs�c�?a?�a��c�?a?�?�?e�?e�?a�s???a��a�?a???�?e�?a?�c�???�a��i?s

//filter?�???�??�?Y?c��a�scs�Optiona�????a?�?�?e�??Y?a?�???a��a��
//a��?z??�?e�?a?�e?�a�za??a?�a??Optiona�za?�i??a?�a?�e?�a�zc�?Optionala��
Optional<String> longName = name.filter((value) -> value.length() > 6);
System.out.println(longName.orElse("The name is less than 6 characters"));//e?�a�?Sanaulla

//a?�a?�a??a?�a�????Optionala�?a???�?e�?filter??�a�scs�???a��a��
Optional<String> anotherName = Optional.of("Sana");
Optional<String> shortName = anotherName.filter((value) -> value.length() > 6);
//e?�a�?i?snamee�?a?�a??e�?6a��c��
System.out.println(shortName.orElse("The name is less than 6 characters"));

a�?a?Si????�a��a��c�?a?�Optionalc��cs�a?�a???�???�a��a?�e??e�se?�a?�a??a�??�?cs�c�?a?�a??c�???�e��a?�a��c�?i?s

public class OptionalDemo {

  public static void main(String[] args) {
    //a?�a�?Optionala�za?�i??a?Ya??a�?e�se?�?�???�e?�a�za�?a?�a?�a��
    Optional<String> name = Optional.of("Sanaulla");

    //a?�a�??????�a�?cs�Optionala�za?�i??a?�a��a�?a??'null'
    Optional empty = Optional.ofNullable(null);

    //isPresent?�???�c�??????�?Y?Optionala�za?�???a?�??�a�?a��
    if (name.isPresent()) {
      //e�?c�?get()e?�a�zOptionala�?a��
      System.out.println(name.get());
    }

    try {
      //a??Optionala�za?�a?Se�?c�?get()?S�a�?NoSuchElementExceptiona��
      System.out.println(empty.get());
    } catch (NoSuchElementException ex) {
      System.out.println(ex.getMessage());
    }

    //ifPresent?�???�?Z?a?�lambdae??e??a??a?�?��a��
    //a��?z?Optionala�?a??a??c�?i??lambdae??e??a??a?sa��c?�a?�a??a��a?S?��e???�?a??a��
    name.ifPresent((value) -> {
      System.out.println("The length of the value is: " + value.length());
    });

    //a��?z???�a�?orElse?�???�a?se?�a�zOptionala�za?�i??a?�a?�e?�a�za?�a�?cs�e��e??a?????a��
    System.out.println(empty.orElse("There is no value present!"));
    System.out.println(name.orElse("There is some value!"));

    //orElseGeta?ZorElsec��a??i??a??a?�a??a?Za?�a�?cs�e�?e��a�?a��
    //orElseGet?Z?a?�lambdae??e??a??c�Y???e�?e��a�?a��
    System.out.println(empty.orElseGet(() -> "Default Value"));
    System.out.println(name.orElseGet(() -> "Default Value"));

    try {
      //orElseThrowa?ZorElse?�???�c��a??i??a??a?�a??a?Ze?�a�za�?a��
      //orElseThrow?S�a�?c��a?�a�?cs�lambdae??e??a??/?�???�c�Y???a?�a??a��
      empty.orElseThrow(ValueAbsentException::new);
    } catch (Throwable ex) {
      System.out.println(ex.getMessage());
    }

    //map?�???�e�se?�a?�a�?cs�lambdae??e??a??a?�?�?Optonala�za?�e�?e��a�?a�� 
    //lambdae??e??a??e?�a�za�?a?sa?�e?�a??Optionala�za?�a��
    Optional<String> upperName = name.map((value) -> value.toUpperCase());
    System.out.println(upperName.orElse("No value found"));

    //flatMapa?Zmapi??Funtioni?�e?za??c�?a??i??a??a?�a??a?Zlambdae??e??a??cs�e?�a�za�?a��
    //map?�???�cs�lambdae??e??a??e?�a�za�?a??a�????a��a?�c��az�i??a?�???e?�a�za�?a?sa?�e?�???Optionala�za?�a��
    //a?�???flatMap?�???�cs�lambdae?�a�za�??��???Optionalc��az�a��
    upperName = name.flatMap((value) -> Optional.of(value.toUpperCase()));
    System.out.println(upperName.orElse("No value found"));

    //filter?�???�??�?Y?Optionaa�????a?�?�?e�?c��a�s???a��a��
    //a��?z??�?e�?e?�a�zOptionala�za?�a�?i??a?�a?�e?�a�zc�?Optionala��
    Optional<String> longName = name.filter((value) -> value.length() > 6);
    System.out.println(longName.orElse("The name is less than 6 characters"));

    //a?�a?�a??c�?a?�i??Optionala�?a???�?e�?c��a�s???a��a��
    Optional<String> anotherName = Optional.of("Sana");
    Optional<String> shortName = anotherName.filter((value) -> value.length() > 6);
    System.out.println(shortName.orElse("The name is less than 6 characters"));

  }

}

a?Se?�a�?c�?e?�a�?a��a?�i?s

Sanaulla
No value present
The length of the value is: 8
There is no value present!
Sanaulla
Default Value
Sanaulla
No value present in the Optional instance
SANAULLA
SANAULLA
Sanaulla
The name is less than 6 characters

关于作者: 高俊阳

(新浪微博:@狐狸叔

查看高俊阳的更多文章 >>



可能感兴趣的文章

发表评论

Comment form

(*) 表示必填项

5 条评论

  1. 九千鸦 说道:

    感谢大神分享

    Well-loved. Like or Dislike: Thumb up 16 Thumb down 0

  2. MeiYT 说道:

    多谢分享

    Thumb up 3 Thumb down 1

  3. kkk 说道:

    不错,跟着 练demo 掌握

    Thumb up 2 Thumb down 0

  4. 带着代码走天涯 说道:

    一直对Optional用法疑惑,看完明白了 谢谢大神

    Thumb up 1 Thumb down 0

  5. wangsaisoon 说道:

    受教 多谢

    Thumb up 1 Thumb down 0

跳到底部
返回顶部