Check list not null

For beginners + This article has a lot of personal opinions.

Operating environment

When I'm doing code reviews, I sometimes see

List list =Some process / method [List is returned]; //NULL and empty check if [list == null || list.size[] == 0] { }

It 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 conclusion

I 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.

List list =Some process / method [List is returned]; //Empty check only if [list.isEmpty[]] { }

Sometimes I come across a "null check" code

Occasionally, 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]{}

bonus

Apparently 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[] method

The 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:

import java.util.ArrayList;

    public static void main[String[] args] {

        List list = new ArrayList[];

            System.out.println["List is null"];

            System.out.println["List is empty"];

            System.out.println["List is not empty or null"];

 
Instead of explicitly checking if the list is null or empty, you can combine the isEmpty[] method with a null check in a single expression, as shown below:

    public static void main[String[] args] {

        List list = Arrays.asList[1, 2, 3];

        if [list == null || list.isEmpty[]] {

            System.out.println["List is empty or null"];

 
Or, even better:

    public static void main[String[] args] {

        List list = Arrays.asList[1, 2, 3];

        if [list != null && !list.isEmpty[]] {

            System.out.println["List is not empty"];

2. Using List.size[] method

Finally, you can use the List.size[] method, which returns the total number of items in it.

    public static void main[String[] args] {

        List list = Arrays.asList[1, 2, 3];

        if [list != null && list.size[] > 0] {

            System.out.println["List is not empty"];

            System.out.println["List is empty or null"];

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 🙂


Video liên quan

Chủ Đề