Statement lambda can be replaced with expression lambda










28















I do user and invitation validation using the Optional facility



@DeleteMapping("/friends/username")
public
HttpEntity<Boolean> removeFriend(
@ApiParam(value = "The user's name", required = true) @PathVariable String username
) {
Long fromId = authorizationService.getUserId();

return userService.findByUsername(username)
.map(user ->
return friendshipService.findFriendship(fromId, user.getId())
.map(friendship ->
friendshipService.removeFriendship(friendship);

friendship.setToId(friendship.getFromId());
friendship.setFromId(friendship.getToId());

friendshipService.removeFriendship(friendship);

return ResponseEntity.ok(true);
).orElseGet(() -> ResponseEntity.notFound().build());
).orElseThrow(() -> new ResourceNotFoundException("User not found"));


However, IntelliJ is colouring my grey return
https://zapodaj.net/2f48b1a26c392.png.html
But when I remove the return, it highlights to me that there is no return https://zapodaj.net/37605f08165c9.png.html



Could someone explain how it works and what is it all about?










share|improve this question


























    28















    I do user and invitation validation using the Optional facility



    @DeleteMapping("/friends/username")
    public
    HttpEntity<Boolean> removeFriend(
    @ApiParam(value = "The user's name", required = true) @PathVariable String username
    ) {
    Long fromId = authorizationService.getUserId();

    return userService.findByUsername(username)
    .map(user ->
    return friendshipService.findFriendship(fromId, user.getId())
    .map(friendship ->
    friendshipService.removeFriendship(friendship);

    friendship.setToId(friendship.getFromId());
    friendship.setFromId(friendship.getToId());

    friendshipService.removeFriendship(friendship);

    return ResponseEntity.ok(true);
    ).orElseGet(() -> ResponseEntity.notFound().build());
    ).orElseThrow(() -> new ResourceNotFoundException("User not found"));


    However, IntelliJ is colouring my grey return
    https://zapodaj.net/2f48b1a26c392.png.html
    But when I remove the return, it highlights to me that there is no return https://zapodaj.net/37605f08165c9.png.html



    Could someone explain how it works and what is it all about?










    share|improve this question
























      28












      28








      28


      6






      I do user and invitation validation using the Optional facility



      @DeleteMapping("/friends/username")
      public
      HttpEntity<Boolean> removeFriend(
      @ApiParam(value = "The user's name", required = true) @PathVariable String username
      ) {
      Long fromId = authorizationService.getUserId();

      return userService.findByUsername(username)
      .map(user ->
      return friendshipService.findFriendship(fromId, user.getId())
      .map(friendship ->
      friendshipService.removeFriendship(friendship);

      friendship.setToId(friendship.getFromId());
      friendship.setFromId(friendship.getToId());

      friendshipService.removeFriendship(friendship);

      return ResponseEntity.ok(true);
      ).orElseGet(() -> ResponseEntity.notFound().build());
      ).orElseThrow(() -> new ResourceNotFoundException("User not found"));


      However, IntelliJ is colouring my grey return
      https://zapodaj.net/2f48b1a26c392.png.html
      But when I remove the return, it highlights to me that there is no return https://zapodaj.net/37605f08165c9.png.html



      Could someone explain how it works and what is it all about?










      share|improve this question














      I do user and invitation validation using the Optional facility



      @DeleteMapping("/friends/username")
      public
      HttpEntity<Boolean> removeFriend(
      @ApiParam(value = "The user's name", required = true) @PathVariable String username
      ) {
      Long fromId = authorizationService.getUserId();

      return userService.findByUsername(username)
      .map(user ->
      return friendshipService.findFriendship(fromId, user.getId())
      .map(friendship ->
      friendshipService.removeFriendship(friendship);

      friendship.setToId(friendship.getFromId());
      friendship.setFromId(friendship.getToId());

      friendshipService.removeFriendship(friendship);

      return ResponseEntity.ok(true);
      ).orElseGet(() -> ResponseEntity.notFound().build());
      ).orElseThrow(() -> new ResourceNotFoundException("User not found"));


      However, IntelliJ is colouring my grey return
      https://zapodaj.net/2f48b1a26c392.png.html
      But when I remove the return, it highlights to me that there is no return https://zapodaj.net/37605f08165c9.png.html



      Could someone explain how it works and what is it all about?







      java spring spring-mvc lambda optional






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 15 '17 at 11:39









      sdfsdsdfsd

      1821210




      1821210






















          2 Answers
          2






          active

          oldest

          votes


















          71














          Your statement lambda



          param -> return expression; 


          can be changed to an expression lambda:



          param -> expression


          Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.






          share|improve this answer

























          • Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?

            – sdfsd
            Sep 15 '17 at 11:52






          • 1





            There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.

            – Seelenvirtuose
            Sep 15 '17 at 11:58


















          0















          Sometimes I found useful to leave the
          braces where they are if the block of code is long enough (I think it improves readability)




          In Android Studio you can locally disable the warning using //noinspection CodeBlock2Expr at the start of the method like in the example below



          //noinspection CodeBlock2Expr
          button.setOnClickListener((View v) ->
          //a long single method call...
          );





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46238702%2fstatement-lambda-can-be-replaced-with-expression-lambda%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            71














            Your statement lambda



            param -> return expression; 


            can be changed to an expression lambda:



            param -> expression


            Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.






            share|improve this answer

























            • Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?

              – sdfsd
              Sep 15 '17 at 11:52






            • 1





              There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.

              – Seelenvirtuose
              Sep 15 '17 at 11:58















            71














            Your statement lambda



            param -> return expression; 


            can be changed to an expression lambda:



            param -> expression


            Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.






            share|improve this answer

























            • Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?

              – sdfsd
              Sep 15 '17 at 11:52






            • 1





              There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.

              – Seelenvirtuose
              Sep 15 '17 at 11:58













            71












            71








            71







            Your statement lambda



            param -> return expression; 


            can be changed to an expression lambda:



            param -> expression


            Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.






            share|improve this answer















            Your statement lambda



            param -> return expression; 


            can be changed to an expression lambda:



            param -> expression


            Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 28 '18 at 11:46

























            answered Sep 15 '17 at 11:46









            SeelenvirtuoseSeelenvirtuose

            16.5k42647




            16.5k42647












            • Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?

              – sdfsd
              Sep 15 '17 at 11:52






            • 1





              There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.

              – Seelenvirtuose
              Sep 15 '17 at 11:58

















            • Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?

              – sdfsd
              Sep 15 '17 at 11:52






            • 1





              There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.

              – Seelenvirtuose
              Sep 15 '17 at 11:58
















            Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?

            – sdfsd
            Sep 15 '17 at 11:52





            Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?

            – sdfsd
            Sep 15 '17 at 11:52




            1




            1





            There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.

            – Seelenvirtuose
            Sep 15 '17 at 11:58





            There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.

            – Seelenvirtuose
            Sep 15 '17 at 11:58













            0















            Sometimes I found useful to leave the
            braces where they are if the block of code is long enough (I think it improves readability)




            In Android Studio you can locally disable the warning using //noinspection CodeBlock2Expr at the start of the method like in the example below



            //noinspection CodeBlock2Expr
            button.setOnClickListener((View v) ->
            //a long single method call...
            );





            share|improve this answer





























              0















              Sometimes I found useful to leave the
              braces where they are if the block of code is long enough (I think it improves readability)




              In Android Studio you can locally disable the warning using //noinspection CodeBlock2Expr at the start of the method like in the example below



              //noinspection CodeBlock2Expr
              button.setOnClickListener((View v) ->
              //a long single method call...
              );





              share|improve this answer



























                0












                0








                0








                Sometimes I found useful to leave the
                braces where they are if the block of code is long enough (I think it improves readability)




                In Android Studio you can locally disable the warning using //noinspection CodeBlock2Expr at the start of the method like in the example below



                //noinspection CodeBlock2Expr
                button.setOnClickListener((View v) ->
                //a long single method call...
                );





                share|improve this answer
















                Sometimes I found useful to leave the
                braces where they are if the block of code is long enough (I think it improves readability)




                In Android Studio you can locally disable the warning using //noinspection CodeBlock2Expr at the start of the method like in the example below



                //noinspection CodeBlock2Expr
                button.setOnClickListener((View v) ->
                //a long single method call...
                );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 22:20

























                answered Nov 13 '18 at 8:31









                MatPagMatPag

                12.6k73651




                12.6k73651



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46238702%2fstatement-lambda-can-be-replaced-with-expression-lambda%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3