Sonar 扫描 The diamond operator (“<>“) should be used Raw types should not be used

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);

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EngineerForSoul

你的鼓励是我孜孜不倦的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值