java 迭代器空集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11373537/
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-10-31 04:48:55  来源:igfitidea点击:

Iterator null collection

javacollectionsiterator

提问by u290629

It's quite common that I have to check null before iterate when not sure the collection reference is null or not. Sample:

当不确定集合引用是否为空时,我必须在迭代之前检查空值是很常见的。样本:

Collection<Object> collection = ...
...
if(collection != null)//troublesome
    for(Object o : collection)

Of course, I know empty collection is much better than null, but in some cases client code cannot control the nullable collection from other modules (for instance, return value from 3rd party code). So I wrote a utility method:

当然,我知道空集合比 null 好得多,但在某些情况下,客户端代码无法控制来自其他模块的可为 null 的集合(例如,来自 3rd 方代码的返回值)。所以我写了一个实用方法:

public static <T> Iterable<T> nullableIterable(Iterable<T> it){
    return it != null ? it : Collections.<T>emptySet();
}

In client code, no need to check null any more:

在客户端代码中,不再需要检查 null:

for(Object o : nullableIterable(collection))
...

Do you think nullableIterable()is reasonable? Any advice? Any concern? Thanks!

你觉得nullableIterable()合理吗?有什么建议吗?有什么顾虑吗?谢谢!

采纳答案by GETah

That looks good. I personally do that too. You will always get developers who would disagree with this as it is kind of defensive programming. Imagine you have a workflow or a class that is not supposed to return null. This means that getting a nullfrom it is a bug which your code will hide as it will turn the nullto an empty collection and the bug will never surface.

看起来不错。我个人也是这样做的。你总会遇到不同意这一点的开发人员,因为它是一种防御性编程。想象一下,您有一个不应返回的工作流或类null。这意味着null从中获取 a是一个错误,您的代码将隐藏该错误,因为它将变成null一个空集合并且该错误永远不会出现。

If you are for example writing APIs that do not support nullcollections then you should avoid this. If client code gives you a nullcollection where you do not support it, you should throw an IllegalArgumentExceptionto let client code know that there is something wrong with the provided collection. Something like:

例如,如果您正在编写不支持null集合的API,那么您应该避免这种情况。如果客户端代码给你一个null你不支持的集合,你应该抛出一个IllegalArgumentException让客户端代码知道提供的集合有问题。就像是:

public void myApiNoSupportForNull(Collection<Object> collection){
   // Pre condition
   if(collection == null) 
     throw new IllegalArgumentException("This API does not support null collections!");
   //...
}

回答by Volodymyr Shtenovych

This looks good to me if you limit usage of this function to the layer that interacts with "external" code and make sure you will never start using it for defense from yourself or from your colleagues. Consider annotate parameters and fields within your code with @Nullable annotation - assuming that what is not annotated cannot be null, pretty helpful especially taking into account that IDEs and static analysis tools are aware of this annotation.

如果您将此功能的使用限制在与“外部”代码交互的层,并确保您永远不会开始使用它来防御您自己或您的同事,这对我来说看起来不错。考虑使用@Nullable 注释来注释代码中的参数和字段 - 假设未注释的内容不能为空,这非常有用,尤其是考虑到 IDE 和静态分析工具知道此注释。

回答by Yair Zaslavsky

In most cases this would be OK.
Bare in mind you might encounter third parties that return null in case of error , and that an empty list is a valid result.
I would therefore consider to alter a bit your code and do something like this:

在大多数情况下,这没问题。
请记住,您可能会遇到在 error 的情况下返回 null 的第三方,并且空列表是有效结果。
因此,我会考虑稍微更改您的代码并执行以下操作:

public static <T> Iterable<T> nullableIterable(Iterable<T> it, boolean exceptionIfNull){
    if (exceptionIfNull && it == null) {
        throw new NUllPointerException("Iterable is null");
    } else
    return it != null ? it : Collections.<T>emptySet();
}

public static <T> Iterable<T> nullableIterable(Iterable<T> it){
    return nul,lableIterable(it,false); //Default behavior for most cases
}