java Optional

For NPE problems, you can use the Optional elegant way to avoid multiple if judgments.

import java.util.Optional;

public class TestNPE {
    public static void main(String[] args) {
        TestDemo testDemo = new TestDemo();
        getCount(testDemo);
    }

    private static void getCount(TestDemo testDemo) {
        //Java8-Optional:优雅,可读性较好
        int count3 = Optional.ofNullable(testDemo).map(TestDemo::getCount).orElse(1);
        System.out.println(count3);

        if(testDemo!=null && testDemo.getCount()!=null){
            System.out.println(testDemo.getCount());
        }
    }

    private static class TestDemo {
        private Integer count;

        public Integer getCount() {
            return count;
        }
    }
}

Note that this is equivalent to judging that testDemo itself is not empty and testDemo.getCount() is not empty.

orElse ()

When the optional value does not exist, call orElse() to return the parameter of orElse(), and return the optional value if the optional value exists

orElseGet ()

When the optional value does not exist, the return value of the interface call in orElseGet() is called. If the optional value exists, the optional value is returned. Examples are as follows:

import java.util.Optional;

public class Main {
    public static void main(String[] args){
        String nullValue = null;
        String optional = Optional.ofNullable(nullValue).orElse("Su");
        System.out.println(optional);
        String optionalGet = Optional.ofNullable(nullValue).orElseGet(() -> "Xiao");
        System.out.println(optionalGet);
        String nonNullOptional = Optional.ofNullable("Susan").orElse("Su");
        System.out.println(nonNullOptional);
        String nonNullOptionalGet = Optional.ofNullable("Molly").orElseGet(() -> "Xiao");
        System.out.println(nonNullOptionalGet);
    }
}

result:

Su
Xiao
Susan
Molly

The difference between orElse() and orElseGet()

First of all, let's compare the examples of using two methods in optional with and without values:

  • Optional has value:
import java.util.Arrays;
import java.util.List;

public class orElseOrElseGetComparation {
    public static void main(String[] args){
        List<Integer> list = Arrays.asList(23,1,3);
        int myElse = list.stream().reduce(Integer::sum).orElse(get("myElse"));
        int myElseGet = list.stream().reduce(Integer::sum).orElseGet(() -> get("myElseGet"));
        System.out.println("myElse的值"+myElse);
        System.out.println("myElseGet的值"+myElseGet);

    }
    public static int get(String name){
        System.out.println(name+"执行了该方法");
        return 1;
    }
}

result:

myElse执行了该方法
myElse的值27
myElseGet的值27
  • When optional is empty

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class orElseOrElseGetComparation {
    public static void main(String[] args){
        List<Integer> list = null;
        List<Integer> myElse = Optional.ofNullable(list).orElse(get("myElseGet"));
        List<Integer> myElseGet = Optional.ofNullable(list).orElseGet(() -> get("myElseGet"));
        System.out.println("myElse的值"+myElse);
        System.out.println("myElseGet的值"+myElseGet);

    }
    public static List<Integer> get(String name){
        System.out.println(name+"执行了该方法");
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        return arr;
    }
}

result:

myElseGet执行了该方法
myElseGet执行了该方法
myElse的值[1]
myElseGet的值[1]

From the above example of the case where optional is empty and has value, you can see that orElse will be executed regardless of whether the optional has a value, orElse and orElseGet will be executed when the optional is empty, and when the optional is not empty, orElseGet Will not be executed.

Reference link:
1. https://www.jianshu.com/p/790f7c185d3e

Guess you like

Origin blog.csdn.net/yangyangrenren/article/details/114691363