是否有理由在 Java 中对泛型进行双重参数化?

Is there a reason to double parametrize generics in Java?

有没有理由这样做:

List<Integer> integers = new ArrayList<Integer>();

与此相对:

List<Integer> integers = new ArrayList<>();

第一种用法我看过几次,好像没什么用,这就引出了一个问题:为什么要用它?

我知道菱形运算符 (<>) 是区分 new LinkedList()new LinkedList<>() 所必需的,只是为了清楚起见。

I've seen the first usage a few times, and it seems to provide no benefit, which begs the question: why use it?

<> 运算符仅在 JDK1.7 中引入,因此您可能已经看到遗留代码,其中使用了 new ArrayList<Integer>()(没有 <> 运算符)

此外,由于向后兼容性,不使用 <> 运算符的代码(如 <Integer>)的支持仍然存在。

所以,答案是如果你的项目使用JDK1.7或更高版本,你就不会使用它。

在 Java 7 引入菱形语法提供的类型推断之前,需要首次使用冗余声明的类型。