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

后端 未结 11 1626
醉话见心
醉话见心 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:45

    Unless you are already using CollectionUtils I would go for List.isEmpty(), less dependencies.

    Performance wise CollectionUtils will be a tad slower. Because it basically follows the same logic but has additional overhead.

    So it would be readability vs. performance vs. dependencies. Not much of a big difference though.

提交回复
热议问题