This question already has an answer here:
这个问题在这里已有答案:
- Google Guava isNullOrEmpty for collections 7 answers
- Google Guava isNullOrEmpty收藏7个答案
Is there any equivalent in google Guava for apache commons' CollectionUtils.isEmpty(list)
to check if a collection is null or empty?
谷歌Guava中是否有任何等效的apache commons的CollectionUtils.isEmpty(列表)来检查集合是否为空或空?
1 个解决方案
#1
5
To check if Collection
is both null and empty, you must do null check by yourself. Guava devs advocate not to use null
s for Collection
s at all - see IdeaGraveyard on Guava Wiki:
要检查Collection是否为空和空,您必须自己进行空检查。 Guava开发人员主张不对集合使用空值 - 请参阅Guava Wiki上的IdeaGraveyard:
Prefer to return empty collections instead of null. Then a plain isEmpty check will suffice.
更喜欢返回空集合而不是null。然后一个简单的isEmpty检查就足够了。
EDIT:
编辑:
See this SO answer by Guava's lead dev.
请参阅Guava的主要开发人员的回答。
#1
5
To check if Collection
is both null and empty, you must do null check by yourself. Guava devs advocate not to use null
s for Collection
s at all - see IdeaGraveyard on Guava Wiki:
要检查Collection是否为空和空,您必须自己进行空检查。 Guava开发人员主张不对集合使用空值 - 请参阅Guava Wiki上的IdeaGraveyard:
Prefer to return empty collections instead of null. Then a plain isEmpty check will suffice.
更喜欢返回空集合而不是null。然后一个简单的isEmpty检查就足够了。
EDIT:
编辑:
See this SO answer by Guava's lead dev.
请参阅Guava的主要开发人员的回答。