0

当我在我的 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>.

还建议说,转换SetList<Object>.

那么如何解决这个问题**如果我需要 Set,返回一个空集合而不是 null 或转换为List<Object>

4

1 回答 1

0

使用Collections.emptySet()而不是Collections.emptyList().

 public Set<CountryModel> getAllCountries() {
     return Collections.emptySet();
 }

有关更多信息,请阅读javadocs

于 2020-10-31T13:26:35.107 回答