当我在我的 Eclipse IDE 中安装 SonarLint 插件时,我遇到了上述(标题)主要问题。
让我们假设:
public List<CountryModel> getAllCountries() {
return null; //***SonarLint Suggesest*: return an empty Collection instead of null.**
}
可以修改(固定)如下:
public List<CountryModel> getAllCountries() {
return Collections.emptyList();
}
注意:同样,我有一组 CountryModel如下,我试图return Collections.emptyList();
public Set<CountryModel> getAllCountries() {
return Collections.emptyList();
}
我得到异常说:
Type mismatch: cannot convert from
List<Object> to Set<CountryModel>.
还建议说,转换Set
为List<Object>
.
那么如何解决这个问题**如果我需要 Set,返回一个空集合而不是 null 或转换为List<Object>
?