Check list not null
For beginners + This article has a lot of personal opinions. Show Operating environmentWhen I'm doing code reviews, I sometimes see ListIt is a code that performs NULL / empty check of List. There is no problem in operation. However, I don't like it so much, so I may ask you to fix it. There are mainly ** two ** reasons. First: Can List be NULL?First of all, I would like you to check if the list variable may be set to NULL in "Some process / method". If you can make it NULL, you should fix the process there. why?This is to reduce the possibility of a NullPointerException. You can also eliminate the caller's null check. If "some process / method" is common, all callers must check for NULL. If the caller does not check for NULL, there is a risk of getting a NullPointerException. If it never returns NULL, the method caller does not have to assume that it will return NULL. The caller implementation will be a little easier. Remove all the elements in the list using remove ()` `` or removeAll ()` ``, etc. Collections.emptyList()Let's return an empty list with something like that.It is also a good idea to add final to the List declaration. * I told you in the comments. # Second: I want you to use isEmpty () instead of size == 0 Is this a personal hobby? opinion? is. #### **`list.size ==List instead of 0.isEmpty()I want you to.`**why?I think there is an opinion that "the contents of ʻis Empty () `` is size == 0 `` !!!". That's right at all. Nothing changes as the processing content. But isn't list.isEmpty ()` `` easier to understand than` `list.size == 0 ...? Perhaps `list.isEmpty ()` is easier for non-Java programmers to understand, "I'm checking if it's empty!". Personal conclusionI want you to stop the NULL check and only check the empty of isEmpty () `` `. To avoid the need for NULL checking, never return NULL in "some process / method", even if it returns an empty list. size() == 0Than,isempty()Is more readable**I personally think**Soisempty()```to use. ListSometimes I come across a "null check" codeOccasionally, there is a code that judges with ``` &&` `` as shown below, but be careful because it will be a NullPointerException if NULL comes. if(list == null && list.size() == 0){}bonusApparently in the training for new employees(list == null || list.size() == 0)It seems that he was taught. I'm not wrong, but ... I was sick of it, so I wrote an article. (What do you mean?
This post will discuss different ways to check whether a list is empty in Java. A list is empty if and only if it contains no elements. 1. Using List.isEmpty() methodThe recommended approach is to use the List.isEmpty() method to check for an empty list in Java. Following is a simple example demonstrating usage of this method:
2. Using List.size() methodFinally, you can use the List.size() method, which returns the total number of items in it.
That’s all about checking whether a list is empty in Java.
Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂 |