Java CollectionUtils.isNotEmpty() 是否比空检查更好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30666524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:57:15  来源:igfitidea点击:

Is CollectionUtils.isNotEmpty() better than a null check?

javacollectionsjava-7java-collections-api

提问by Trying

Many advice to use CollectionUtils.isNotEmpty(coll)rather then coll != nullin the below use case also.

在下面的用例中使用CollectionUtils.isNotEmpty(coll)而不是的许多建议coll != null

if (CollectionUtils.isNotEmpty(coll)) {
    for (String str : coll) {
    }
}

instead of

代替

if (coll != null) {
    for (String str : coll) {
    }
}

Is there any reason/advantage to use CollectionUtils.isNotEmpty(coll)instead of other here? Thanks.

有什么理由/优势可以CollectionUtils.isNotEmpty(coll)代替其他在这里使用吗?谢谢。

回答by NaN

The issue is, that the collection can still be empty, when it is not null. So, in your case it depends on your preferences what you choose.

问题是,当集合不为空时,集合仍然可以为空。因此,就您而言,这取决于您的喜好选择。

回答by M Anouti

No real advantages here. Even if there is, it would be extremely small. It just prevents the creation of an Iteratorand executing a branch instruction, that's all there is to it.

这里没有真正的优势。就算有,也太小了。它只是阻止创建Iterator和执行分支指令,仅此而已。

This small advantage occurs only when the collection is empty. The following loop:

这个小优势只有在集合为空时才会出现。以下循环:

for (String str : coll) {
   ...
}

is equivalent to:

相当于:

for (Iterator<String> iterator = col.iterator(); iterator.hasNext();) {
   String str = iterator.next();
   ...
}

When the collection is empty, the check on CollectionUtils.isNotEmpty(coll)prevents the loop from executing. Hence no Iteratoris created in memory and no call to hasNext()is done. This is at the expense to a O(1)call to coll.isEmpty().

当集合为空时,检查会CollectionUtils.isNotEmpty(coll)阻止循环执行。因此Iterator在内存中创建no并且没有调用 to hasNext()。这是以O(1)调用 为代价的coll.isEmpty()

回答by taanielo

Decompiling reveals

反编译显示

public static boolean isEmpty(Collection coll) {
    return coll == null || coll.isEmpty();
}

回答by hariprasad

As explained above it depends, what you want to test and how is your logic constructed.

如上所述,这取决于您要测试的内容以及您的逻辑是如何构建的。

Suppose your example

假设你的例子

if (CollectionUtils.isNotEmpty(coll)) {
  for (String str : coll) {
     System.out.println("Branch 1. Collection is not empty.");
  }
}
else {
  System.out.println("Branch 2. Collection is empty.");
}

In this example, we can see, that alwaysBranch1 or Branch2 is executed.

在这个例子中,我们可以看到,总是执行 Branch1 或 Branch2。

If we use null expression, the result will be different if collis not null but empty

如果我们使用空表达式,如果coll不是空而是空,结果会有所不同

if (coll != null) {
  for (String str : coll) {
     System.out.println("Branch1. Collection is not empty.");
  }
}
else {
  System.out.println("Branch2. Collection is empty.");
}

If the collection collis not null but it is empty, nor Branch1 either Branch2 is executed, because the condition coll != nullis true, but in loop forthere is not even one pass.

如果集合coll不为空但为空,也不执行 Branch1 或 Branch2,因为条件coll != null为真,但在循环中for甚至没有通过。

Of course, ifexpression coll != null && coll.isNotEmpty()doing the same work as CollectionUtils.isNotEmpty(coll).

当然,if表达式coll != null && coll.isNotEmpty()CollectionUtils.isNotEmpty(coll).

Therefore it not advisable programming manner to use test on null in case of collections coll != nullonly. This is a case of poorly treated extreme conditions, which may be a source of unwanted result.

因此,coll != null仅在集合的情况下对 null 使用测试不是可取的编程方式。这是一个处理不当的极端条件的情况,这可能是不想要的结果的来源。