您的位置:首页 > 编程语言 > Java开发

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

2016-03-01 11:24 555 查看

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<>();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: