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

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

    To Check collection is empty, you can use method: .count(). Example:

    DBCollection collection = mMongoOperation.getCollection("sequence");
        if(collection.count() == 0) {
            SequenceId sequenceId = new SequenceId("id", 0);
            mMongoOperation.save(sequenceId);
        }
    

提交回复
热议问题