<bdo id='qMevB'></bdo><ul id='qMevB'></ul>
  1. <i id='qMevB'><tr id='qMevB'><dt id='qMevB'><q id='qMevB'><span id='qMevB'><b id='qMevB'><form id='qMevB'><ins id='qMevB'></ins><ul id='qMevB'></ul><sub id='qMevB'></sub></form><legend id='qMevB'></legend><bdo id='qMevB'><pre id='qMevB'><center id='qMevB'></center></pre></bdo></b><th id='qMevB'></th></span></q></dt></tr></i><div id='qMevB'><tfoot id='qMevB'></tfoot><dl id='qMevB'><fieldset id='qMevB'></fieldset></dl></div>

  2. <tfoot id='qMevB'></tfoot>
    <legend id='qMevB'><style id='qMevB'><dir id='qMevB'><q id='qMevB'></q></dir></style></legend>

    <small id='qMevB'></small><noframes id='qMevB'>

      SONAR:将此 lambda 替换为方法引用

      SONAR: Replace this lambda with a method reference(SONAR:将此 lambda 替换为方法引用)
      <tfoot id='PQLnV'></tfoot>

        <legend id='PQLnV'><style id='PQLnV'><dir id='PQLnV'><q id='PQLnV'></q></dir></style></legend>
        <i id='PQLnV'><tr id='PQLnV'><dt id='PQLnV'><q id='PQLnV'><span id='PQLnV'><b id='PQLnV'><form id='PQLnV'><ins id='PQLnV'></ins><ul id='PQLnV'></ul><sub id='PQLnV'></sub></form><legend id='PQLnV'></legend><bdo id='PQLnV'><pre id='PQLnV'><center id='PQLnV'></center></pre></bdo></b><th id='PQLnV'></th></span></q></dt></tr></i><div id='PQLnV'><tfoot id='PQLnV'></tfoot><dl id='PQLnV'><fieldset id='PQLnV'></fieldset></dl></div>
              <tbody id='PQLnV'></tbody>

              <small id='PQLnV'></small><noframes id='PQLnV'>

              • <bdo id='PQLnV'></bdo><ul id='PQLnV'></ul>

              • 本文介绍了SONAR:将此 lambda 替换为方法引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                Sonar 告诉我用方法引用替换这个 lambda"

                Sonar tells me "Replace this lambda with a method reference"

                public class MyClass {
                
                    private List<SomeValue> createSomeValues(List<Anything> anyList) {
                        return anyList //
                               .stream() //
                               .map(anything -> createSomeValue(anything)) //
                               .collect(Collectors.toList());
                   }
                
                    private SomeValue createSomeValue(Anything anything) {
                        StatusId statusId = statusId.fromId(anything.getStatus().getStatusId());
                        return new SomeValue(anything.getExternId(), statusId);
                    }
                
                }
                

                这可能吗?我尝试了几件事,比如

                Is this possible here? I tried several things, like

                .map(MyClass::createSomeValue) //
                

                但是我需要将方法更改为静态.而且我不是静态方法的忠实粉丝.

                but I need to change the method to static then. And I am not a big fan of static methods.

                SonarQube 的解释是:

                Explanation of SonarQube is:

                方法/构造函数引用比使用 lambda 更紧凑和可读,因此是首选.

                Method/constructor references are more compact and readable than using lambdas, and are therefore preferred.

                推荐答案

                是的,你可以使用 this::createSomeValue:

                private List<SomeValue> createSomeValues(List<Anything> anyList) {
                    return anyList //
                            .stream() //
                            .map(this::createSomeValue) //
                            .collect(Collectors.toList());
                }
                

                这种方法参考称为"对特定对象的实例方法的引用".在这种情况下,您指的是实例 this 的方法 createSomeValue.

                This kind of method reference is called "Reference to an instance method of a particular object". In this case, you are referring to the method createSomeValue of the instance this.

                使用 lambda 表达式是否更好"是一个见仁见智的问题.但是,您可以参考 this answer/brian-goetz">Brian Goetz 解释了为什么首先在语言中添加方法引用.

                Whether it is "better" or not that using a lambda expression is a matter of opinion. However, you can refer to this answer written by Brian Goetz that explains why method-references were added in the language in the first place.

                这篇关于SONAR:将此 lambda 替换为方法引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                Security - Array is stored directly(安全性 - 数组直接存储)
                SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)
                <legend id='xsV31'><style id='xsV31'><dir id='xsV31'><q id='xsV31'></q></dir></style></legend>
              • <i id='xsV31'><tr id='xsV31'><dt id='xsV31'><q id='xsV31'><span id='xsV31'><b id='xsV31'><form id='xsV31'><ins id='xsV31'></ins><ul id='xsV31'></ul><sub id='xsV31'></sub></form><legend id='xsV31'></legend><bdo id='xsV31'><pre id='xsV31'><center id='xsV31'></center></pre></bdo></b><th id='xsV31'></th></span></q></dt></tr></i><div id='xsV31'><tfoot id='xsV31'></tfoot><dl id='xsV31'><fieldset id='xsV31'></fieldset></dl></div>
              • <tfoot id='xsV31'></tfoot>

                      <small id='xsV31'></small><noframes id='xsV31'>

                      • <bdo id='xsV31'></bdo><ul id='xsV31'></ul>
                          <tbody id='xsV31'></tbody>