java 带有静态泛型的类型安全、泛型、空集合

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

Type-safe, generic, empty Collections with static generics

javacollections

提问by Droo

I return empty collections vs. null whenever possible. I switch between two methods for doing so using java.util.Collections:

我尽可能返回空集合与 null。我使用java.util.Collections在两种方法之间切换:

return Collections.EMPTY_LIST;
return Collections.emptyList();

where emptyList()is supposed to be type-safe. But I recently discovered:

whereemptyList()应该是类型安全的。但我最近发现:

return Collections.<ComplexObject> emptyList();
return Collections.<ComplexObject> singletonList(new ComplexObject());

etc.

等等。

I see this method in Eclipse Package Explorer:

我在 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!!

但我在源代码 (1.5) 中看不到这是如何完成的。这个神奇的傻瓜是怎么发生的!!

EDIT: How is the static Generic type accomplished?

编辑:静态泛型类型是如何实现的?

采纳答案by Bert F

EDIT: How is the static Generic type accomplished?

编辑:静态泛型类型是如何实现的?

http://www.docjar.com/html/api/java/util/Collections.java.html

http://www.docjar.com/html/api/java/util/Collections.java.html

public class Collections {
    ...
    public static final List EMPTY_LIST = new EmptyList<Object>();
    ...
    public static final <T> List<T> emptyList() {
        return (List<T>) EMPTY_LIST;
    }
    ...
}

You can see the link for the implementation of the EmptyList class if you're curious, but for your question, it doesn't matter.

如果您好奇,可以查看 EmptyList 类的实现链接,但对于您的问题,这并不重要。

回答by Powerlord

return Collections.<ComplexObject> emptyList();

Using this will get rid of warnings from Eclipse about non-generic collections.

使用它可以消除来自 Eclipse 的关于非泛型集合的警告。

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.

话虽如此,由于空列表是不可变的并且 Java 在编译时擦除泛型类型,因此有类型的空列表在功能上将与无类型的空列表相同。

回答by Itay Maman

<clinit>is the static initializer block. It is a block of code which is executed exactly once (when the class is loaded).

<clinit>是静态初始化块。它是一个只执行一次的代码块(当类被加载时)。

So, instead of writing

所以,而不是写

class  A  {
   static int x = 5;
}

One can write:

一个人可以写:

class A {
   static int x;

   static {  // static initializer starts
      x = 5; 
   }
}

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.

这两个类是等价的。在静态初始化块中,可以放置任意代码,从而使用复杂计算的结果初始化静态字段。

回答by erickson

<clinit>is the name of the method into which the class initialization code is collected by during compilation. (That is, all of the code inside static {}blocks, and the initializers of static members, in source code order.)

<clinit>是在编译期间收集类初始化代码的方法的名称。(也就是说,static {}块内的所有代码,以及静态成员的初始值设定项,按源代码顺序排列。)

It has nothing to do with explicit type parameters in method invocations.

它与方法调用中的显式类型参数无关。