I have something like List<List<UsersDetails>> userList
. If I debug to see its value it's giving [[]]
i.e., List<UsersDetails>
is empty and List<List<UsersDetails>>
is also empty. Is there a way to check if List<UsersDetails>
is empty without iteration?
I tried userList.sizeOf
, userList.empty()
functions and userList==null
operator but all are giving false.
If you want to check each element in the "outer" list you have to iterate over it somehow. Java 8's streams would hide this from you, though, and provide a slightly cleaner syntax:
boolean allEmpty = userList.stream().allMatch(l -> l == null || l.empty());
There is not. There is:
if (userList.isEmpty() || userList.get(0).isEmpty()) { ... }
But mostly if the notion: "This a list of lists where the list of lists contains 1 list, but that list is empty" is something you should consider as 'empty', you're using the wrong datastructure. You haven't explained what you are modelling with this List<List<UsersDetails>>
but perhaps if you elaborate on that, some other data type in java.* or perhaps guava would be far more suitable. For example, maybe a Map<Integer, UsersDetail>
is a better match here (mapping a user's ID to their details).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With