Java Lambdas and Closures

后端 未结 3 934
星月不相逢
星月不相逢 2020-12-29 06:20

I hear lambdas are coming soon to a Java near you (J8). I found an example of what they will look like on some blog:

SoccerService soccerService = (teamA, te         


        
3条回答
  •  自闭症患者
    2020-12-29 06:36

    See this page for a full version of that example (however, the relevant parts are shown below).

    The types are inferred from the SoccerService interface and SoccerResult enum, not shown in your snippet:

    enum SoccerResult{
        WON, LOST, DRAW
    }
    
    interface SoccerService {
        SoccerResult getSoccerResult(Integer teamA, Integer teamB);
    }
    

    The benefit (of lambdas versus standard anonymous classes) is just reduced verbosity:

    (x, y) => x + y
    

    versus something like:

    new Adder()
    {
      public int add(int x, int y)
      {
        return x + y;
      }
    }
    

    For the difference between a closure and a lambda, see this question.

提交回复
热议问题