How to check if a list is empty or null in Java

2 Answers

0 votes
import java.util.ArrayList;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<Integer> list = new ArrayList<>();
 
        if (list == null || list.isEmpty()) {
            System.out.println("Empty or null");
        } else {
            System.out.println("Not Empty or null");
        }
    }
}



/*
run:

Empty or null

*/

 



answered Jul 2, 2022 by avibootz
0 votes
import java.util.Collections;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        List<Integer> list = Collections.<Integer>emptyList();  
 
        if (list == null || list.isEmpty()) {
            System.out.println("Empty or null");
        } else {
            System.out.println("Not Empty or null");
        }
    }
}



/*
run:

Empty or null

*/

 



answered Jul 2, 2022 by avibootz

Related questions

...