Sir*_*nce 0 java lambda method-reference sonarlint sonarlint-eclipse
我有一个包含错误列表的集合.我想通过密钥(UUID UserId)对它们进行分组.为此,我复制了这个答案的代码:https: //stackoverflow.com/a/30202075/4045364
Collection<FilterError> filterErrors = new ArrayList<FilterError>();
// ... some filterErrors get added to the collection ...
return filterErrors.stream().collect(Collectors.groupingBy(w -> w.getUserId()));
Run Code Online (Sandbox Code Playgroud)
Sonar Lint给了我以下错误:
用方法引用替换此lambda.
->
我尝试过的:
基于这些问题:SONAR:用方法引用和Runable接口替换此lambda:用方法引用替换此lambda.(sonar.java.source未设置.假设为8或更高.)
filterErrors.stream().collect(Collectors.groupingBy(this::getUserId()));
Run Code Online (Sandbox Code Playgroud)
基于这个问题:用方法引用'Objects :: nonNull'替换这个lambda
filterErrors.stream().collect(Collectors.groupingBy(UUID::getUserId()));
Run Code Online (Sandbox Code Playgroud)
两者都给出错误:
此表达式的目标类型必须是功能接口
有没有办法解决这个SonarLint问题?
小智 5
您需要使用流所针对的对象的类名.例:
List<String> list = ...;
list.stream().collect(Collectors.groupingBy(String::toUpperCase));
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下:
FilterError::getUserId
Run Code Online (Sandbox Code Playgroud)