Google Guava isNullOrEmpty 对于集合

发布于 2024-11-27 14:12:57 字数 395 浏览 2 评论 0原文

我看到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉城凉梦凉人心 2024-12-04 14:12:57

不,这种方法在 Guava 中不存在,实际上在我们的“想法墓地”中。

我们不认为“是否为 null 或为空”是您真正想要询问集合的问题。

如果一个集合可能为 null,并且 null 应该被视为与空相同,那么请预先消除所有歧义,如下所示:

Set<Foo> foos = NaughtyClass.getFoos();
if (foos == null) {
  foos = ImmutableSet.of();
}

或如下(如果您愿意):

Set<Foo> foos = MoreObjects.firstNonNull(
    NaughtyClass.getFoos(), ImmutableSet.<Foo>of());

之后,您可以只使用 .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:

Set<Foo> foos = NaughtyClass.getFoos();
if (foos == null) {
  foos = ImmutableSet.of();
}

or like this (if you prefer):

Set<Foo> foos = MoreObjects.firstNonNull(
    NaughtyClass.getFoos(), ImmutableSet.<Foo>of());

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.

很糊涂小朋友 2024-12-04 14:12:57

在整个 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 nulls. The authors want to discourage you from using null as much as you probably do, and providing utility methods to make it easier to use null 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 of null. Or as a parameter to a function that is expecting no null-valued lists, consider using Preconditions.checkNotNull, which throws an exception if a null is (unexpectedly) passed in.

If null really is legitimate, list == null || list.isEmpty() is not that hard.

沫尐诺 2024-12-04 14:12:57

有一个 CollectionUtils.isEmpty() 在公共集合中。

There's a CollectionUtils.isEmpty() in commons-collections.

她比我温柔 2024-12-04 14:12:57

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.

陪我终i 2024-12-04 14:12:57

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

江南月 2024-12-04 14:12:57

我的解决方案是: MoreObjects.firstNonNull(list, Collections.emptyList())

我正在将 Guava MoreObjects 与 JDK Collections 一起使用。

 @Test
public void listnull() {
List<String> list = null;

for (String str : MoreObjects.firstNonNull(list, Collections.<String> emptyList())) {
    System.out.println(str);
}

list = new ArrayList<String>();
list.add("http://stackoverflow.com/");

for (String str : MoreObjects.firstNonNull(list, Collections.<String> emptyList())) {
    System.out.println(str);
}
}

My solution is : MoreObjects.firstNonNull(list, Collections. emptyList())

I am using Guava MoreObjects with JDK Collections.

 @Test
public void listnull() {
List<String> list = null;

for (String str : MoreObjects.firstNonNull(list, Collections.<String> emptyList())) {
    System.out.println(str);
}

list = new ArrayList<String>();
list.add("http://stackoverflow.com/");

for (String str : MoreObjects.firstNonNull(list, Collections.<String> emptyList())) {
    System.out.println(str);
}
}
只有一腔孤勇 2024-12-04 14:12:57

看看阿帕奇收藏
CollectionUtils.isEmpty()
如果集合为 null 或为空,则返回 true

Look at Appache Collections
CollectionUtils.isEmpty()
returns true if collection is null or empty

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文