Meaning of lambda () -> in Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
15
down vote

favorite
1












I am looking at the following Stack Overflow answer:
How to change Spring's @Scheduled fixedDelay at runtime



And in the code there is the following line:



schedulerFuture = taskScheduler.schedule(() -> , this);


I would like to know what the lambda () -> means in that code. I need to write it without using lambdas.










share|improve this question



















  • 25




    It's a Runnable that does nothing.
    – ernest_k
    yesterday










  • first argument is runnable, so you are passing anonymous class of instance Runnable in which run method does nothing i.e. is empty.
    – SMA
    yesterday







  • 3




    without lambda, you would need to pass an anonymous Runnable with empty body, like new Runnable() @Override public void run()
    – Alex Salauyou
    yesterday















up vote
15
down vote

favorite
1












I am looking at the following Stack Overflow answer:
How to change Spring's @Scheduled fixedDelay at runtime



And in the code there is the following line:



schedulerFuture = taskScheduler.schedule(() -> , this);


I would like to know what the lambda () -> means in that code. I need to write it without using lambdas.










share|improve this question



















  • 25




    It's a Runnable that does nothing.
    – ernest_k
    yesterday










  • first argument is runnable, so you are passing anonymous class of instance Runnable in which run method does nothing i.e. is empty.
    – SMA
    yesterday







  • 3




    without lambda, you would need to pass an anonymous Runnable with empty body, like new Runnable() @Override public void run()
    – Alex Salauyou
    yesterday













up vote
15
down vote

favorite
1









up vote
15
down vote

favorite
1






1





I am looking at the following Stack Overflow answer:
How to change Spring's @Scheduled fixedDelay at runtime



And in the code there is the following line:



schedulerFuture = taskScheduler.schedule(() -> , this);


I would like to know what the lambda () -> means in that code. I need to write it without using lambdas.










share|improve this question















I am looking at the following Stack Overflow answer:
How to change Spring's @Scheduled fixedDelay at runtime



And in the code there is the following line:



schedulerFuture = taskScheduler.schedule(() -> , this);


I would like to know what the lambda () -> means in that code. I need to write it without using lambdas.







java lambda java-8 java-7 java-9






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 15 hours ago









Boann

35.6k1184116




35.6k1184116










asked yesterday









Aliuk

303517




303517







  • 25




    It's a Runnable that does nothing.
    – ernest_k
    yesterday










  • first argument is runnable, so you are passing anonymous class of instance Runnable in which run method does nothing i.e. is empty.
    – SMA
    yesterday







  • 3




    without lambda, you would need to pass an anonymous Runnable with empty body, like new Runnable() @Override public void run()
    – Alex Salauyou
    yesterday













  • 25




    It's a Runnable that does nothing.
    – ernest_k
    yesterday










  • first argument is runnable, so you are passing anonymous class of instance Runnable in which run method does nothing i.e. is empty.
    – SMA
    yesterday







  • 3




    without lambda, you would need to pass an anonymous Runnable with empty body, like new Runnable() @Override public void run()
    – Alex Salauyou
    yesterday








25




25




It's a Runnable that does nothing.
– ernest_k
yesterday




It's a Runnable that does nothing.
– ernest_k
yesterday












first argument is runnable, so you are passing anonymous class of instance Runnable in which run method does nothing i.e. is empty.
– SMA
yesterday





first argument is runnable, so you are passing anonymous class of instance Runnable in which run method does nothing i.e. is empty.
– SMA
yesterday





3




3




without lambda, you would need to pass an anonymous Runnable with empty body, like new Runnable() @Override public void run()
– Alex Salauyou
yesterday





without lambda, you would need to pass an anonymous Runnable with empty body, like new Runnable() @Override public void run()
– Alex Salauyou
yesterday













4 Answers
4






active

oldest

votes

















up vote
35
down vote



accepted










Its a Runnable with an empty run definition. The anonymous class representation of this would be:



new Runnable() 
@Override public void run()
// could have done something here







share|improve this answer


















  • 2




    Related: stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is () -> and not Runnable.noop, from one of the Java designers).
    – yshavit
    15 hours ago

















up vote
2
down vote













Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name.



Lets take a look at this example.



(int a, int b) -> return a > b



In your case, you can do something like below:



schedulerFuture = taskScheduler.schedule(new Runnable() 
@Override
public void run()
// task details

, this);





share|improve this answer





























    up vote
    1
    down vote













    For lambdas:



    Left side is arguments, what you take. Enclosed in () are all the arguments this function takes



    -> indicates that it's a function that takes what's on the left and passes it on to the right for processing



    Right side is the body - what the lambda does. Enclosed in is everything this function does



    After you figure that out you only need to know that that construction passes an instance of matching class (look at what's the expected argument type in the schedule() call) with it's only method doing exactly the same as the lambda expression we've just analyzed.






    share|improve this answer



























      up vote
      -1
      down vote













      Lambda expressions basically express instances of functional interfaces.
      In a way Lambda expression will be: (lambda operator params) -> body



      () -> System.out.println("This means Lambda expression is not taking any parameters");



      (p) -> System.out.println("Lambda expression with one parameter: " + p);






      share|improve this answer




















      • This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are.
        – Andrejs
        18 hours ago










      • @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> means in that code. I need to write it without using lambdas." He as asked for the meaning of () ->
        – Archit Sud
        6 hours ago











      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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f52292953%2fmeaning-of-lambda-in-java%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      35
      down vote



      accepted










      Its a Runnable with an empty run definition. The anonymous class representation of this would be:



      new Runnable() 
      @Override public void run()
      // could have done something here







      share|improve this answer


















      • 2




        Related: stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is () -> and not Runnable.noop, from one of the Java designers).
        – yshavit
        15 hours ago














      up vote
      35
      down vote



      accepted










      Its a Runnable with an empty run definition. The anonymous class representation of this would be:



      new Runnable() 
      @Override public void run()
      // could have done something here







      share|improve this answer


















      • 2




        Related: stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is () -> and not Runnable.noop, from one of the Java designers).
        – yshavit
        15 hours ago












      up vote
      35
      down vote



      accepted







      up vote
      35
      down vote



      accepted






      Its a Runnable with an empty run definition. The anonymous class representation of this would be:



      new Runnable() 
      @Override public void run()
      // could have done something here







      share|improve this answer














      Its a Runnable with an empty run definition. The anonymous class representation of this would be:



      new Runnable() 
      @Override public void run()
      // could have done something here








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 18 hours ago









      dun

      295




      295










      answered yesterday









      nullpointer

      30.4k1054127




      30.4k1054127







      • 2




        Related: stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is () -> and not Runnable.noop, from one of the Java designers).
        – yshavit
        15 hours ago












      • 2




        Related: stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is () -> and not Runnable.noop, from one of the Java designers).
        – yshavit
        15 hours ago







      2




      2




      Related: stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is () -> and not Runnable.noop, from one of the Java designers).
      – yshavit
      15 hours ago




      Related: stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is () -> and not Runnable.noop, from one of the Java designers).
      – yshavit
      15 hours ago












      up vote
      2
      down vote













      Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name.



      Lets take a look at this example.



      (int a, int b) -> return a > b



      In your case, you can do something like below:



      schedulerFuture = taskScheduler.schedule(new Runnable() 
      @Override
      public void run()
      // task details

      , this);





      share|improve this answer


























        up vote
        2
        down vote













        Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name.



        Lets take a look at this example.



        (int a, int b) -> return a > b



        In your case, you can do something like below:



        schedulerFuture = taskScheduler.schedule(new Runnable() 
        @Override
        public void run()
        // task details

        , this);





        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name.



          Lets take a look at this example.



          (int a, int b) -> return a > b



          In your case, you can do something like below:



          schedulerFuture = taskScheduler.schedule(new Runnable() 
          @Override
          public void run()
          // task details

          , this);





          share|improve this answer














          Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name.



          Lets take a look at this example.



          (int a, int b) -> return a > b



          In your case, you can do something like below:



          schedulerFuture = taskScheduler.schedule(new Runnable() 
          @Override
          public void run()
          // task details

          , this);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Prashant Goswami

          1035




          1035




















              up vote
              1
              down vote













              For lambdas:



              Left side is arguments, what you take. Enclosed in () are all the arguments this function takes



              -> indicates that it's a function that takes what's on the left and passes it on to the right for processing



              Right side is the body - what the lambda does. Enclosed in is everything this function does



              After you figure that out you only need to know that that construction passes an instance of matching class (look at what's the expected argument type in the schedule() call) with it's only method doing exactly the same as the lambda expression we've just analyzed.






              share|improve this answer
























                up vote
                1
                down vote













                For lambdas:



                Left side is arguments, what you take. Enclosed in () are all the arguments this function takes



                -> indicates that it's a function that takes what's on the left and passes it on to the right for processing



                Right side is the body - what the lambda does. Enclosed in is everything this function does



                After you figure that out you only need to know that that construction passes an instance of matching class (look at what's the expected argument type in the schedule() call) with it's only method doing exactly the same as the lambda expression we've just analyzed.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  For lambdas:



                  Left side is arguments, what you take. Enclosed in () are all the arguments this function takes



                  -> indicates that it's a function that takes what's on the left and passes it on to the right for processing



                  Right side is the body - what the lambda does. Enclosed in is everything this function does



                  After you figure that out you only need to know that that construction passes an instance of matching class (look at what's the expected argument type in the schedule() call) with it's only method doing exactly the same as the lambda expression we've just analyzed.






                  share|improve this answer












                  For lambdas:



                  Left side is arguments, what you take. Enclosed in () are all the arguments this function takes



                  -> indicates that it's a function that takes what's on the left and passes it on to the right for processing



                  Right side is the body - what the lambda does. Enclosed in is everything this function does



                  After you figure that out you only need to know that that construction passes an instance of matching class (look at what's the expected argument type in the schedule() call) with it's only method doing exactly the same as the lambda expression we've just analyzed.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  Deltharis

                  1,64911125




                  1,64911125




















                      up vote
                      -1
                      down vote













                      Lambda expressions basically express instances of functional interfaces.
                      In a way Lambda expression will be: (lambda operator params) -> body



                      () -> System.out.println("This means Lambda expression is not taking any parameters");



                      (p) -> System.out.println("Lambda expression with one parameter: " + p);






                      share|improve this answer




















                      • This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are.
                        – Andrejs
                        18 hours ago










                      • @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> means in that code. I need to write it without using lambdas." He as asked for the meaning of () ->
                        – Archit Sud
                        6 hours ago















                      up vote
                      -1
                      down vote













                      Lambda expressions basically express instances of functional interfaces.
                      In a way Lambda expression will be: (lambda operator params) -> body



                      () -> System.out.println("This means Lambda expression is not taking any parameters");



                      (p) -> System.out.println("Lambda expression with one parameter: " + p);






                      share|improve this answer




















                      • This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are.
                        – Andrejs
                        18 hours ago










                      • @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> means in that code. I need to write it without using lambdas." He as asked for the meaning of () ->
                        – Archit Sud
                        6 hours ago













                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      Lambda expressions basically express instances of functional interfaces.
                      In a way Lambda expression will be: (lambda operator params) -> body



                      () -> System.out.println("This means Lambda expression is not taking any parameters");



                      (p) -> System.out.println("Lambda expression with one parameter: " + p);






                      share|improve this answer












                      Lambda expressions basically express instances of functional interfaces.
                      In a way Lambda expression will be: (lambda operator params) -> body



                      () -> System.out.println("This means Lambda expression is not taking any parameters");



                      (p) -> System.out.println("Lambda expression with one parameter: " + p);







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered yesterday









                      Archit Sud

                      725




                      725











                      • This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are.
                        – Andrejs
                        18 hours ago










                      • @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> means in that code. I need to write it without using lambdas." He as asked for the meaning of () ->
                        – Archit Sud
                        6 hours ago

















                      • This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are.
                        – Andrejs
                        18 hours ago










                      • @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> means in that code. I need to write it without using lambdas." He as asked for the meaning of () ->
                        – Archit Sud
                        6 hours ago
















                      This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are.
                      – Andrejs
                      18 hours ago




                      This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are.
                      – Andrejs
                      18 hours ago












                      @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> means in that code. I need to write it without using lambdas." He as asked for the meaning of () ->
                      – Archit Sud
                      6 hours ago





                      @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> means in that code. I need to write it without using lambdas." He as asked for the meaning of () ->
                      – Archit Sud
                      6 hours ago


















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52292953%2fmeaning-of-lambda-in-java%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      One-line joke