gpt4 book ai didi

java - SonarLint:用方法引用替换此 lambda

转载 作者:行者123 更新时间:2023-12-01 16:45:51 27 4
gpt4 key购买 nike

我有一个包含错误列表的集合。我想通过键(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()));

声纳 Lint 给我以下错误:

Replace this lambda with a method reference. ->

我尝试过的:

基于这些问题:SONAR: Replace this lambda with a method referenceRunable Interface : Replace this lambda with a method reference. (sonar.java.source not set. Assuming 8 or greater.)

filterErrors.stream().collect(Collectors.groupingBy(this::getUserId()));

基于这个问题:Replace this lambda with method reference 'Objects::nonNull'

filterErrors.stream().collect(Collectors.groupingBy(UUID::getUserId()));

两者都给出错误:

The target type of this expression must be a functional interface

有什么办法可以解决这个 SonarLint 问题吗?

最佳答案

您需要使用流所针对的对象的类名称。示例:

List<String> list = ...;
list.stream().collect(Collectors.groupingBy(String::toUpperCase));

所以在你的情况下:

FilterError::getUserId

关于java - SonarLint:用方法引用替换此 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51205654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com