To check if a list is not empty in Java, you can use the isEmpty()
method of the List
interface.
In this example,
myList
with some elements.myList
is not empty by calling the isEmpty()
method and negating it.import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> myList = Arrays.asList(1, 2, 3);
if (!myList.isEmpty()) {
System.out.println("The list is not empty");
} else {
System.out.println("The list is empty");
}
}
}
The list is not empty
In this example,
emptyList
without any elements.emptyList
is not empty by calling the isEmpty()
method and negating it.import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> emptyList = new ArrayList<>();
if (!emptyList.isEmpty()) {
System.out.println("The list is not empty");
} else {
System.out.println("The list is empty");
}
}
}
The list is empty
In this tutorial, we learned How to check if a List is Not Empty in Java language with well detailed examples.