具有静态泛型的类型安全、泛型、空集合
我尽可能返回空集合和 null。我使用 java.io 在两种方法之间切换。 util.Collections:
return Collections.EMPTY_LIST;
return Collections.emptyList();
其中 emptyList()
应该是类型安全的。但我最近发现:
return Collections.<ComplexObject> emptyList();
return Collections.<ComplexObject> singletonList(new ComplexObject());
等等。
我在 Eclipse Package Explorer 中看到了这个方法:
<clinit> () : void
但我没有看到这是如何在源代码(1.5)中完成的。这种神奇的愚蠢行为是怎么发生的!!
编辑: 静态泛型类型是如何实现的?
I return empty collections vs. null whenever possible. I switch between two methods for doing so using java.util.Collections:
return Collections.EMPTY_LIST;
return Collections.emptyList();
where emptyList()
is supposed to be type-safe. But I recently discovered:
return Collections.<ComplexObject> emptyList();
return Collections.<ComplexObject> singletonList(new ComplexObject());
etc.
I see this method in Eclipse Package Explorer:
<clinit> () : void
but I don't see how this is done in the source code (1.5). How is this magic tomfoolerie happening!!
EDIT:
How is the static Generic type accomplished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用它可以消除 Eclipse 中关于非泛型集合的警告。
话虽如此,由于空列表是不可变的并且 Java 在编译时擦除泛型类型,因此类型化空列表在功能上与非类型化空列表相同。
Using this will get rid of warnings from Eclipse about non-generic collections.
Having said that, a typed empty list is going to be functionally identical to an untyped empty list due to empty list being immutable and Java erasing generic types at compile time.
http://www.docjar.com/html/api/java /util/Collections.java.html
如果您好奇,您可以查看 EmptyList 类的实现链接,但对于您的问题,这并不重要。
http://www.docjar.com/html/api/java/util/Collections.java.html
You can see the link for the implementation of the EmptyList class if you're curious, but for your question, it doesn't matter.
是静态初始化块。它是只执行一次的代码块(当加载类时)。因此,
可以不用写 One 来写:
这两个类是等价的。在静态初始化块内,可以放置任意代码,从而使用复杂计算的结果初始化静态字段。
<clinit>
is the static initializer block. It is a block of code which is executed exactly once (when the class is loaded).So, instead of writing
One can write:
These two classes are equivalent. Inside a static initializer block one can place arbitrary code and thus initialize static fields with the results of complicated calculations.
是编译期间收集类初始化代码的方法的名称。 (也就是说,static {}
块内的所有代码以及静态成员的初始值设定项,按源代码顺序排列。)它与方法调用中的显式类型参数无关。
<clinit>
is the name of the method into which the class initialization code is collected by during compilation. (That is, all of the code insidestatic {}
blocks, and the initializers of static members, in source code order.)It has nothing to do with explicit type parameters in method invocations.
对于那些正在寻找类似解决方案的人,但对于当前时间(2023 年 4 月),人们已经开始使用
List.of()
作为Collections.emptyList 的简洁替代品()
...以同样的方式,类型作为
Collections.emptyList();
中方法的前缀使用相同的方法前缀模式:
List .of();
For those who are looking for a similar solution, but for more current time (2023/Apr) where one has moved on to using
List.of()
as a terse replacement forCollections.emptyList()
...In the same way the type is prefixed to the method in
Collections.<ComplexObject>emptyList();
Use the same method-prefix pattern:
List.<ComplexObject>of();