concept lambda in category java

appears as: lambda, lambdas, lambda, The lambda
Modern Java in Action: Lambdas, streams, reactive and functional programming

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.

As well as allowing (named) methods to be first-class values, Java 8 allows a richer idea of functions as values, including lambdas[4] (or anonymous functions). For example, you can now write (int x) -> x + 1 to mean “the function that, when called with argument x, returns the value x + 1.” You might wonder why this is necessary, because you could define a method add1 inside a class MyMathsUtils and then write MyMaths-Utils::add1! Yes, you could, but the new lambda syntax is more concise for cases where you don’t have a convenient method and class available. Chapter 3 explores lambdas in detail. Programs using these concepts are said to be written in functional-programming style; this phrase means “writing programs that pass functions around as first-class values.”

In the first eight chapters of this book, you saw the expressive power of lambdas and the Streams API. You were mainly creating new code that used these features. If you have to start a new Java project, you can use lambdas and streams immediately.

  • Passing a lambda (a one-off piece of code) such as
    apple -> apple.getWeight() > 150
  • Functional Programming in Java: How functional techniques improve your Java programs

    This is an excerpt from Manning's book Functional Programming in Java: How functional techniques improve your Java programs.

    Note that the lambda is the only place where Java allows you to use the (x, y) notation for tuples. Unfortunately, it can’t be used in any other cases, such as returning a tuple from a function.

    2.2.5. Simplifying the code by using lambdas

    The second problem you have is that functions defined using anonymous classes are cumbersome to use in coding. If you’re using Java 5 to 7, you’re out of luck, because there’s no other way to go. Fortunately, Java 8 introduced lambdas.

    Java 8 in Action: Lambdas, streams, and functional-style programming

    This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.

    As well as allowing (named) methods to be first-class values, Java 8 allows a richer idea of functions as values, including lambdas[4] (or anonymous functions). For example, you can now write (int x) -> x + 1 to mean “the function that, when called with argument x, returns the value x + 1.” You might wonder why this is necessary because you could define a method add1 inside a class MyMathsUtils and then write MyMaths-Utils::add1! Yes, you could, but the new lambda syntax is more concise for cases where you don’t have a convenient method and class available. Chapter 3 explores lambdas in detail. Programs using these concepts are said to be written in functional-programming style—this phrase means “writing programs that pass functions around as first-class values.”

    Listing 3.1. Valid lambda expressions in Java 8
    (String s) -> s.length() #1 (Apple a) -> a.getWeight() > 150 #2 (int x, int y) -> { System.out.println("Result:"); System.out.println(x+y); } #3 () -> 42 #4 (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()) #5 #1: The first lambda expression has one parameter of type String and returns an int. The lambda doesn’t have a return statement here because the return is implied. #2: The second lambda expression has one parameter of type Apple and returns a boolean (whether the apple is heavier than 150 g). #3: The third lambda expression has two parameters of type int with no return (void return). Note that lambda expressions can contain multiple statements, in this case two. #4: The fourth lambda expression has no parameter and returns an int. #5: The fifth lambda expression has two parameters of type Apple and returns an int: the comparison of the weight of the two Apples.

    In the first seven chapters of this book, you saw the expressive power of lambdas and the Streams API. You were mainly creating new code that made use of these features. This is great if you have to start a new Java project—you can use lambdas and streams immediately.