Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utility to check null and return size as 0 if empty for a collection/list in java

Tags:

java

I have list which can be null sometimes, the return type should be int ex:

int size = 0; 
if(list != null) {
    size = list.size;
} 

Could you please let me know whether any utility is available?

Thanks.

like image 714
user2323036 Avatar asked Oct 12 '25 08:10

user2323036


1 Answers

What about

int size = (list == null) ? 0 : list.size();

I'm not sure why you would want a utility for this, since the above seems simple enough.

like image 83
arshajii Avatar answered Oct 15 '25 00:10

arshajii