How does Java use optional and stream to replace if NULL judgment logic (jdk8 and above)
Through this article, you can replace the null check in business logic with very short code, and it is easy to log or other operations when null pointers appear.
Note: if you are not familiar with the lambda expression and stream in the new features of Java 8, you can supplement the foundation and understand the concept.
Firstly, the list in the following code puts many person objects, some of which are null. If you call the getxxx () method of person without verification, you will certainly report a null pointer error. Generally, we adopt the scheme of adding if judgment:
public class DemoUtils { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person()); personList.add(null); personList.add(new Person("小明",10)); personList.add(new Person("小红",12)); for (Person person : personList) { //if判空逻辑 if (person != null) { System.out.println(person.getName()); System.out.println(person.getAge()); } } } static class Person { private String name; private int age; public Person() { } public Person(String name,int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }
In fact, the new Java features stream API and optional provide more elegant methods:
Use the filter in the stream API to filter out empty objects in the queue. Filter (objects:: nonnull) means that each element in the list executes the nonnull () method of objects, the elements that return false are filtered out, and the elements that return true are retained.
public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person()); personList.add(null); personList.add(new Person("小明",12)); personList.stream().filter(Objects::nonNull).forEach(person->{ System.out.println(person.getName()); System.out.println(person.getAge()); }); }
The personlist itself in the example may also be null. If the business logic requires a log alarm when the personlist is null, it can be implemented with optional Elegance:
public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person()); personList.add(null); personList.add(new Person("小明",10)); personList.add(new Person("小红",12)); Optional.ofNullable(personList).orElseGet(() -> { System.out.println("personList为null!"); return new ArrayList<>(); }).stream().filter(Objects::nonNull).forEach(person -> { System.out.println(person.getName()); System.out.println(person.getAge()); }); }
In code
orElseGet(() -> { //代替log System.out.println("personList为null!"); return new ArrayList<>(); })
Indicates that if the personlist is null, execute these two sentences of code to return a list without elements. In this way, when the personlist is null, no null pointer error will be reported, and a log will be logged.
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. Thank you for your support.