To check if a List is empty in Java, you can use the isEmpty() method of the List interface.
In this example,
myList
.isEmpty()
method of the List interface myList
to check if it is empty.isEmpty()
method returns true if the list is empty, false otherwise.import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<>();
boolean empty = myList.isEmpty();
System.out.println("Is list empty: " + empty);
}
}
Is list empty: true
In this example,
myList
.isEmpty()
method of the List interface myList
to check if it is empty.isEmpty()
method returns true if the list is empty, false otherwise.import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<>();
myList.add(1);
boolean empty = myList.isEmpty();
System.out.println("Is list empty: " + empty);
}
}
Is list empty: false
In this tutorial, we learned How to check if a List is Empty in Java language with well detailed examples.