Checking if a collection is empty in Java: which is the best method?

后端 未结 11 1613
醉话见心
醉话见心 2020-12-01 01:27

I have two ways of checking if a List is empty or not

if (CollectionUtils.isNotEmpty(listName)) 

and

if (listName != null          


        
11条回答
  •  庸人自扰
    2020-12-01 01:40

    CollectionUtils.isNotEmpty checks if your collection is not null and not empty. This is better comparing to double check but only if you have this Apache library in your project. If you don't then use:

    if(list != null && !list.isEmpty())
    

提交回复
热议问题