1: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.
Java 7引入了diamond操作符(<>)来减少泛型代码的冗长。例如,不必在列表的声明和构造函数中声明列表的类型,现在可以使用<>简化构造函数声明,编译器将推断类型。
Note that this rule is automatically disabled when the project's sonar.java.source is lower than 7.
注意,该规则在项目的sonar.java中被自动禁用。来源低于7。
2:Raw types should not be used
Generic types shouldn't be used raw (without type parameters) in variable declarations or return values. Doing so bypasses generic type checking, and defers the catch of unsafe code to runtime.
泛型类型不应该在变量声明或返回值中使用原始(没有类型参数)。这样做可以绕过泛型类型检查,并将不安全代码的捕获延迟到运行时。
3:该如何写才能让sonar满意呢?
sonar 解决了,代码报红。看下图
正确编辑如下: 不要省略HashMap后面的<> 和5
Map<String, Object> map = new HashMap<>(5);