Best practice to validate null and empty collection in Java

后端 未结 9 2005
北海茫月
北海茫月 2020-12-12 10:57

I want to verify whether a collection is empty and null. Could anyone please let me know the best practice.

Currently, I am checking as below:



        
9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 11:17

    For all the collections including map use: isEmpty method which is there on these collection objects. But you have to do a null check before:

    Map map;
    
    ........
    if(map!=null && !map.isEmpty())
    ......
    

提交回复
热议问题