Skip to content

Java Lambda expressions

So, what are lambda expressions, anyway? Think of them as nifty shortcuts for writing certain types of methods in Java. They're like little blocks of code that you can pass around just like you would any other piece of data.

Imagine you need to do something simple, like doubling a number or checking if a string contains a certain letter. Instead of writing a whole new method with a bunch of lines of code, you can use a lambda expression to do it in just a few lines. It's like using a mini-program within your program!

Let’s break it down a bit more. Say you have a list of numbers, and you want to do something to each of them, like multiplying each number by 2. With lambda expressions, you can do this in a snap without writing a loop or a separate method. It’s like saying, “Hey Java, for each number in my list, here’s what I want you to do…” and Java takes care of the rest.

Lambda expressions are not only handy but also make your code cleaner and easier to read. They’re like little signposts that say, “Hey, look here! This is what I’m doing with this piece of code.” Plus, they can make your code more flexible because you can easily pass them around and use them in different parts of your program.

Now, you might be wondering, “Okay, this sounds cool and all, but how do I actually use lambda expressions?” Well, it’s simpler than you might think. You just need to know a little bit of syntax, and you’re good to go. Once you get the hang of it, you’ll be using lambda expressions like a pro!

Syntax

Syntax: (parameters) -> { statements; }

  1. Parameter List: This comes before the arrow -> and specifies the parameters that your lambda expression will accept. If there are no parameters, you use empty parentheses ().
  2. Arrow ->: This separates the parameter list from the body of the lambda expression.
  3. Body: This is where you define the behavior of your lambda expression. It can be a single statement or a block of code enclosed in curly braces {}.

Examples:

Example 1: Simple Lambda Expression

java
// Syntax: () -> expression
Runnable runnable = () -> System.out.println("Hello, lambda!");
runnable.run(); // Output: Hello, lambda!

In this example, we create a lambda expression that implements the Runnable interface. It takes no parameters and simply prints "Hello, lambda!" when invoked.

Example 2: Lambda Expression with Parameters

java
// Syntax: (parameters) -> expression
MathOperation addition = (int a, int b) -> a + b;
int result = addition.operate(10, 5); // Result: 15

Here, we define a lambda expression addition that takes two integer parameters and returns their sum.

Example 3: Lambda Expression with Multiple Statements

java
// Syntax: (parameters) -> { statements; }
MathOperation subtraction = (int a, int b) -> {
    int result = a - b;
    System.out.println("Subtraction result: " + result);
    return result;
};

This lambda expression subtracts two numbers and prints the result along with a message.

Example 4: Lambda Expression with Predicate

java
// Syntax: (parameter) -> expression or statement(s)
Predicate<Integer> isEven = num -> num % 2 == 0;
boolean result = isEven.test(6); // Result: true

Here, we define a lambda expression isEven that checks if a number is even.

See how concise and expressive lambda expressions can be? They allow you to write code that's more readable and maintainable, all while reducing boilerplate.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.