<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 替换为方法引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                <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>