Collections.emptyList() 和 Collections.EMPTY_LIST 有什么区别 [英] What is the difference between Collections.emptyList() and Collections.EMPTY_LIST
本文介绍了Collections.emptyList() 和 Collections.EMPTY_LIST 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Java 中,我们有 Collections.emptyList() 和 集合.EMPTY_LIST.两者具有相同的属性:
In Java, we have Collections.emptyList() and Collections.EMPTY_LIST. Both have the same property:
返回空列表(不可变).这个列表是可序列化的.
Returns the empty list (immutable). This list is serializable.
那么使用一种或另一种的确切区别是什么?
So what is the exact difference between using the one or the other?
推荐答案
Collections.EMPTY_LIST
返回旧式List
Collections.emptyList()
使用类型推断,因此返回列表
Collections.EMPTY_LIST
returns an old-styleList
Collections.emptyList()
uses type-inference and therefore returnsList<T>
Collections.emptyList() 是在 Java 1.5 中添加的,它可能总是更可取的.这样,您就无需在代码中进行不必要的转换.
Collections.emptyList() was added in Java 1.5 and it is probably always preferable. This way, you don't need to unnecessarily cast around within your code.
Collections.emptyList()
本质上是为你做演员.
@SuppressWarnings("unchecked")
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
这篇关于Collections.emptyList() 和 Collections.EMPTY_LIST 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文