This is the 16th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Writing in the front
We all know that Java8 Optional has solved the problem of if NULL. But this is really no trouble back at home, whether there will be a Java. Lang. NullPointerException, we look at today
Let’s start with a test class
public class ListTest {
static List<User> list =null;
public static void main(String[]args) {
/ / 01
System.out.println(list.isEmpty());//
/ /.System.out.println(CollectionUtils.isEmpty(list)); }}Copy the code
Throw questions: which will appear above the 01 and 02 Java lang. NullPointerException, give the answer at the end
Null pointer nightmare
java.lang.NullPointerException
Copy the code
This screen is every programmer encountered, relatively low and inevitable, the reason is relatively low, because such a low-level error should not occur, inevitable for the following two reasons
One is because the business logic is not called short, the other is because there are no pits that are not taken into account (we will focus on this today)
Let’s start by looking at a piece of code that queries a user entity by id
Java8 was like this before
public User getById(Integer userId) {
List<User> list = mapper.selectById(userId);
if(list ! =null) {return list.get(0);
}
return;
}
Copy the code
Java8 operation
public User getById(Integer userId) {
List<User> list = mapper.selectById(userId);
return Optional.ofNullable(list).map(user -> user.get(0)).orElse(null);
}
Copy the code
If a list is not null, it is 0. If a list is not null, it is 0. If a list is null, it is 0.
If a list is 0, ofNullable may fail, but an object will not fail.
Colleagues see after a meal operation, we look at the solution:
CollectionUtils
Yeah, that’s right using the collections utility class in Spring. Look at the code
public User getById(Integer userId) {
List<User> list = mapper.selectById(userId);
returnOptional.ofNullable(list) .filter(u -> ! CollectionUtils.isEmpty(list)) .map(user -> user.get(0)).orElse(null);
}
Copy the code
The filter layer is filtered by collectionUtils. isEmpty (null or 0). NullPointerException does not appear when the map gets it. In this way, everything is safe.
So you can use this in normal code as well
List<User> list = mapper.selectById(userId);
if(! CollectionUtils.isEmpty(list)){// Execute logic
}
Copy the code
Let’s take a look at his source code
public abstract class CollectionUtils {
/**
* Return {@code true} if the supplied Collection is {@code null} or empty.
* Otherwise, return {@code false}.
* @param collection the Collection to check
* @return whether the given Collection is empty
*/
public static boolean isEmpty(@NullableCollection<? > collection) {
return (collection == null|| collection.isEmpty()); }}Copy the code
As you can see, it’s just a tool class. I did a few things for you.
conclusion
The answers to 01 and 02 at the beginning of the article come naturally.
01 is empty, 02 is safe.
So that’s it for today, we’re going to be good at finding these tools to help us solve some problems.
It’s worth asking why Java doesn’t incorporate a better tooling approach. Or some of the tool classes and other good solutions that you know of, feel free to talk in the comments
overtones
Thank you for reading, and if you feel like you’ve learned something, you can like it and follow it. Also welcome to have a question we comment below exchange
Come on! See you next time!
To share with you a few I wrote in front of a few SAO operation
Copy object, this operation is a little SAO!
Dry! SpringBoot uses listening events to implement asynchronous operations