我可以在没有UncheckedException的情况下使用Collections.EMPTY_LIST吗?

时间:2022-09-09 19:45:57

Is there a Generics Friendly way of using Collection.EMPTY_LIST in my Java Program.

在我的Java程序中是否有使用Collection.EMPTY_LIST的Generics Friendly方法。

I know I could just declare one myself, but I'm just curious to know if there's a way in the JDK to do this.

我知道我可以自己声明一个,但我只是想知道JDK中是否有办法做到这一点。

Something like users = Collections<User>.EMPTY_LIST;

像users = Collections .EMPTY_LIST;

3 个解决方案

#1


31  

By doing the following:

通过执行以下操作:

List<User> users = Collections.emptyList();

The type of the returned list from Collections.emptyList(); will be inferred as a String due to the left-hand-side of the assignment. However, if you prefer to not have this inference, you can define it explicitly by doing the following:

Collections.emptyList()返回列表的类型;由于赋值的左侧,将被推断为String。但是,如果您不想进行此推断,可以通过执行以下操作明确定义它:

List<User> users = Collections.<User>emptyList(); 

In this particular instance, this may appear as redundant to most people (in fact, I've seen very little code out in the wild that makes use of explicit type arguments), however for a method with the signature: void doStuff(List<String> users) it would be perfectly clean for one to invoke doStuff() with an explicit type argument as follows:

在这个特定的例子中,对于大多数人来说这可能看起来是多余的(事实上,我在野外看到很少使用显式类型参数的代码),但是对于具有签名的方法:void doStuff(List < String> users)如果用一个显式类型参数调用doStuff(),那将是完全干净的,如下所示:

doStuff(Collections.<String>emptyList());

#2


1  

List<User> users = Collections.emptyList();

#3


1  

After creating the empty list, I would recommend storing it as a constant rather than creating a new one each time.

创建空列表后,我建议将其存储为常量,而不是每次都创建一个新列表。

Also, there are performance benefits to using Collections.emptyList() versus new ArrayList(0), although the difference is probably small. The list returned by emptyList() is optimized to be an immutable empty list. For example, the size() method simply returns 0, rather than a field lookup or whatever ArrayList does.

此外,使用Collections.emptyList()与新的ArrayList(0)有性能优势,尽管差异可能很小。 emptyList()返回的列表被优化为不可变的空列表。例如,size()方法只返回0,而不是字段查找或ArrayList所做的任何操作。

#1


31  

By doing the following:

通过执行以下操作:

List<User> users = Collections.emptyList();

The type of the returned list from Collections.emptyList(); will be inferred as a String due to the left-hand-side of the assignment. However, if you prefer to not have this inference, you can define it explicitly by doing the following:

Collections.emptyList()返回列表的类型;由于赋值的左侧,将被推断为String。但是,如果您不想进行此推断,可以通过执行以下操作明确定义它:

List<User> users = Collections.<User>emptyList(); 

In this particular instance, this may appear as redundant to most people (in fact, I've seen very little code out in the wild that makes use of explicit type arguments), however for a method with the signature: void doStuff(List<String> users) it would be perfectly clean for one to invoke doStuff() with an explicit type argument as follows:

在这个特定的例子中,对于大多数人来说这可能看起来是多余的(事实上,我在野外看到很少使用显式类型参数的代码),但是对于具有签名的方法:void doStuff(List < String> users)如果用一个显式类型参数调用doStuff(),那将是完全干净的,如下所示:

doStuff(Collections.<String>emptyList());

#2


1  

List<User> users = Collections.emptyList();

#3


1  

After creating the empty list, I would recommend storing it as a constant rather than creating a new one each time.

创建空列表后,我建议将其存储为常量,而不是每次都创建一个新列表。

Also, there are performance benefits to using Collections.emptyList() versus new ArrayList(0), although the difference is probably small. The list returned by emptyList() is optimized to be an immutable empty list. For example, the size() method simply returns 0, rather than a field lookup or whatever ArrayList does.

此外,使用Collections.emptyList()与新的ArrayList(0)有性能优势,尽管差异可能很小。 emptyList()返回的列表被优化为不可变的空列表。例如,size()方法只返回0,而不是字段查找或ArrayList所做的任何操作。