+ 1
What does "List is Empty" mean in Java, Kotlin?
I have encountered before a "List is empty" error on Kotlin and so far, other sources are scarce, and lack details. Can someone help me out?
4 odpowiedzi
+ 2
Try to check your list with a boolean variable like boolean b = mylist.isEmpty();
+ 1
CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases.
isNotEmpty() method of CollectionUtils can be used to check if a list is not empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list.
For Example:
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
public class CollectionUtilsTester {
public static void main(String[] args) {
List<String> list = getList();
System.out.println("Non-Empty List Check: " + checkNotEmpty1(list));
System.out.println("Non-Empty List Check: " + checkNotEmpty1(list));
}
static List<String> getList() {
return null;
}
static boolean checkNotEmpty1(List<String> list) {
return !(list == null || list.isEmpty());
}
static boolean checkNotEmpty2(List<String> list) {
return CollectionUtils.isNotEmpty(list);
}
}