在 Java 中,我们有 Collections.emptyList()和 Collections.EMPTY_LIST .两者具有相同的属性:
Returns the empty list (immutable). This list is serializable.
那么使用其中一个或另一个的确切区别是什么?
最佳答案
-
Collections.EMPTY_LIST
返回旧式List
-
Collections.emptyList()
使用类型推断,因此返回List<T>
Collections.emptyList() 是在 Java 1.5 中添加的,它可能总是更可取。这样,您就无需在代码中进行不必要的转换。
Collections.emptyList()
本质上是为你做 Actor 。
@SuppressWarnings("unchecked")
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
关于java - Collections.emptyList() 和 Collections.EMPTY_LIST 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14870819/