What does "List is Empty" mean in Java, Kotlin? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

1st Mar 2020, 3:34 PM
Jhan Edilbert P. Domingo
Jhan Edilbert P. Domingo - avatar
2 Answers
+ 2
Try to check your list with a boolean variable like boolean b = mylist.isEmpty();
1st Mar 2020, 8:40 PM
ERMAC
+ 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); } }
2nd Mar 2020, 3:15 AM
ROBIN C R
ROBIN C R - avatar