Google Guava isNullOrEmpty 对于集合
我看到 Guava 对字符串有 isNullOrEmpty 实用方法,
Strings.isNullOrEmpty(str)
我们对列表有类似的方法吗?类似的东西
Lists.isNullOrEmpty(list)
应该相当于
list == null || list.isEmpty()
另外,我们有类似的数组吗?类似的东西
Arrays.isNullOrEmpty(arr)
应该相当于
arr == null || arr.length == 0
I see that Guava has isNullOrEmpty utility method for Strings
Strings.isNullOrEmpty(str)
Do we have anything similar for Lists? Something like
Lists.isNullOrEmpty(list)
which should be equivalent to
list == null || list.isEmpty()
Also, do we have anything similar for Arrays? Something like
Arrays.isNullOrEmpty(arr)
which should be equivalent to
arr == null || arr.length == 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,这种方法在 Guava 中不存在,实际上在我们的“想法墓地”中。
我们不认为“是否为 null 或为空”是您真正想要询问集合的问题。
如果一个集合可能为 null,并且 null 应该被视为与空相同,那么请预先消除所有歧义,如下所示:
或如下(如果您愿意):
之后,您可以只使用
.isEmpty()
像平常一样。在调用顽皮的 API 后立即执行此操作,您就可以将奇怪的事情抛在脑后,而不是让它无限期地继续下去。如果“实际上意味着空集合的 null”没有返回给您,而是传递给您,那么您的工作很简单:只需抛出一个
NullPointerException
,然后让调用者成形。No, this method does not exist in Guava and is in fact in our "idea graveyard."
We don't believe that "is null or empty" is a question you ever really want to be asking about a collection.
If a collection might be null, and null should be treated the same as empty, then get all that ambiguity out of the way up front, like this:
or like this (if you prefer):
After that, you can just use
.isEmpty()
like normal. Do this immediately upon calling the naughty API and you've put the weirdness behind you, instead of letting it continue on indefinitely.And if the "null which really means empty collection" is not being returned to you, but passed to you, your job is easy: just let a
NullPointerException
be thrown, and make that caller shape up.在整个 Guava 中您会发现的一件事是,它们往往非常反对
null
。作者希望阻止您尽可能多地使用null
,并且提供实用方法来使null
更容易使用会适得其反。如果您想使用 Guava 的范例,请考虑此集合(或数组)的源是否真的应该选择返回
null
。如果不是,请考虑将其标记为@NonNull
并返回空集合而不是null
。或者,作为不需要null
值列表的函数的参数,请考虑使用Preconditions.checkNotNull
,如果null
则抛出异常(意外地)传入。如果
null
确实合法,则list == null || list.isEmpty()
并不难。One thing you will tend to find throughout Guava is that they tend to be very antagonistic toward
null
s. The authors want to discourage you from usingnull
as much as you probably do, and providing utility methods to make it easier to usenull
would be counterproductive against this end.If you want to use Guava's paradigm, consider if the source of this collection (or array) really ought to optionally return
null
. If not, consider marking it@NonNull
and return empty collections instead ofnull
. Or as a parameter to a function that is expecting nonull
-valued lists, consider usingPreconditions.checkNotNull
, which throws an exception if anull
is (unexpectedly) passed in.If
null
really is legitimate,list == null || list.isEmpty()
is not that hard.有一个 CollectionUtils.isEmpty() 在公共集合中。
There's a CollectionUtils.isEmpty() in commons-collections.
Spring 框架有一个专门的 util 类,称为 CollectionUtils。您正在寻找的方法是:
org.springframework.util.CollectionUtils.isEmpty
。对于 null 和空集合,它返回 true。对于数组,有 org.springframework.util.ObjectUtils.isEmpty 方法,其行为几乎相同。
Spring Framework has specialized util class called
CollectionUtils
. And the method you are looking for is:org.springframework.util.CollectionUtils.isEmpty
. It returns true for null and empty collections.And for arrays there is
org.springframework.util.ObjectUtils.isEmpty
method which behaves pretty the same.Apache CollectionUtils 4 有一个方法 CollectionUtils.emptyIfNull() 如果集合为 null,则返回空列表。这在 foreach 循环中非常有用,因此您不需要在迭代之前进行 null 检查
Apache CollectionUtils 4 has a method CollectionUtils.emptyIfNull() that returns a empty list if the collection is null. This is very useful in a foreach loop, so you dont need to do a null check before iterating
我的解决方案是: MoreObjects.firstNonNull(list, Collections.emptyList())
我正在将 Guava MoreObjects 与 JDK Collections 一起使用。
My solution is : MoreObjects.firstNonNull(list, Collections. emptyList())
I am using Guava MoreObjects with JDK Collections.
看看阿帕奇收藏
CollectionUtils.isEmpty()
如果集合为 null 或为空,则返回 true
Look at Appache Collections
CollectionUtils.isEmpty()
returns true if collection is null or empty