使用集合接口的时候应该使用通用类型代替具体的实现类型

Sonar静态代码检查发现下面缺陷:

Declarations should use Java collection interfaces such as “List” rather than specific implementation classes such as “LinkedList”

The purpose of the Java Collections API is to provide a well defined hierarchy of interfaces in order to hide all implementation details.
Implementating classes must be used to instantiate a new collection, but the result of an instantiation should immediately be stored in a variable whose type is a
Java Collection interface

就是说譬如你要实现一个HashMap的集合,不用直接用 HashMap hm = new HashMap();这样的方式去实例化,而是要用 Map hm = new HashMap();来做。
同理LinkedList的实例化也是 List ll = new LinkedList();这样的方式去实例化

你可能感兴趣的:(使用集合接口的时候应该使用通用类型代替具体的实现类型)