java8 泛型声明 The diamond operator ("<>") should be used

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java8 泛型声明 The diamond operator ("<>") should be used相关的知识,希望对你有一定的参考价值。

 

The diamond operator ("<>") should be used

Java 7 introduced the diamond operator (<>) to reduce the verbosity of generics code. For instance, instead of having to declare a List‘s type in both its declaration and its constructor, you can now simplify the constructor declaration with <>, and the compiler will infer the type.

Note that this rule is automatically disabled when the project‘s java.source.version is lower than 7.

Noncompliant Code Example

List<String> strings = new ArrayList<String>();  // Noncompliant
Map<String,List<Integer>> map = new HashMap<String,List<Integer>>();  // Noncompliant

Compliant Solution

List<String> strings = new ArrayList<>();
Map<String,List<Integer>> map = new HashMap<>();

以上是关于java8 泛型声明 The diamond operator ("<>") should be used的主要内容,如果未能解决你的问题,请参考以下文章

java8泛型

Java8基础之泛型

Java8基础知识泛型

Java 8 新特性:3-函数(Function)接口

Java8基础知识泛型的约束与局限性

具有泛型类型的 Java 8 方法参考