【发布时间】:2021-02-13 14:08:06
【问题描述】:
当我在我的 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> ?
【问题讨论】:
-
你可以使用
Collections.emptySet()
标签: java collections set sonarlint-eclipse