1. Optional函数:

  1.1 Optional.of(T value)

    当value为空时,会报nullpointexception

  1.2 Optional.ofNullable(T value) 

    当value为空时创建空实例,不为空时创建optional实例

  1.3 isPresent()

    判断是否包含值

   Optional<T> opt=Optional.ofNullable(T value); if(opt.isPresent){...}else{...}

  1.4 orElse(T value)

    如果调用对象包含值则返回值,否则返回orElse()中value  

public class OptionalTest {
    public static void main(String[] args) {
        Apple apple1=new Apple();
        Apple apple2=new Apple();
        apple1.setColor("red");
        apple1.setWeight(3.0);
        List<Apple> appleList= Arrays.asList(apple1,apple2);
        orElseTest(appleList);
    }

    public static void orElseTest(List<Apple> appleList){
        Apple ap=new Apple();
        ap.setWeight(0.0);
        ap.setColor("none");
        for(Apple apple:appleList){
            String color=Optional.ofNullable(apple.getColor()).orElse(ap.getColor());
            System.out.println("color is:"+color);
        }
    }
}

result:
color is:red
color is:none
View Code

 

  1.5 orElseGet(Supplier s)

    如果调用对象包含值则返回值,否则返回执行s后获取的值

public class OptionalTest {
    public static void main(String[] args) {
        Apple apple1=new Apple();
        Apple apple2=new Apple();
        apple1.setColor("red");
        apple1.setWeight(3.0);
        List<Apple> appleList= Arrays.asList(apple1,apple2);
        orElseGetTest(appleList);
    }

    public static void orElseGetTest(List<Apple> appleList){

        for(Apple apple:appleList){
            String color=Optional.ofNullable(apple.getColor()).orElseGet(()->{
                Apple apple1=new Apple();
                apple1.setColor("none");
                return "a haha no color";
            });
            System.out.println("color is:"+color);
        }
    }
}
result:
color is:red
color is:a haha no color
View Code

  1.6 orElseThrow();

    如果调用对象包含值则返回,否则抛出异常;orElseThrow(()->new IllegalStateException("This is an exception"));

  1.7 map(Function f)

    如果有值调用f进行处理,否则返回Optional.empty();这个和java8中map()一样,对其中参数进行函数操作

public class OptionalTest {
    public static void main(String[] args) {
        Apple apple1=new Apple();
        Apple apple2=new Apple();
        apple1.setColor("red");
        apple1.setWeight(3.0);
        List<Apple> appleList= Arrays.asList(apple1,apple2);
        mapTest(appleList);
    }

    public static void mapTest(List<Apple> appleList){
        Apple ap=new Apple();
        ap.setWeight(0.0);
        ap.setColor("none");
        for(Apple apple:appleList){
            Optional<String> color=Optional.ofNullable(apple.getColor());
            System.out.println("optional result:"+color);
            color.map(c->{
                c=c+" is test for map";
                System.out.println("======"+c);
                return c;
            });
        }
    }
}
result:
optional result:Optional[red]
======red is test for map

optional result:Optional.empty
View Code

 

  1.8 flatMap(Function f)

    如果有值调用f进行处理,否则返回Optional.empty(),但要求返回值必须时Optional

  1.9 empty()

    判断为null时返回empty对象,相当于Optional.ofNullable()后没有orElse()或者orElseget()

public class OptionalTest {
    public static void main(String[] args) {
        Apple apple1=new Apple();
        Apple apple2=new Apple();
        apple1.setColor("red");
        apple1.setWeight(3.0);
        List<Apple> appleList= Arrays.asList(apple1,apple2);
        emptyTest(appleList);
    }

    public static void emptyTest(List<Apple> appleList){
        for(Apple apple:appleList){
            Optional<String> color=Optional.ofNullable(apple.getColor());
            System.out.println(color);
        }
    }
}
result:
Optional[red]
Optional.empty
View Code

 

posted on 2020-06-11 16:41  colorfulworld  阅读(2361)  评论(0编辑  收藏  举报