“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

🌊 author’s home page: Haichong 🌊 Author Profile: πŸ₯‡HDZ core group member, πŸ† full stack quality creator, 🌊 has been ranked top ten in the weekly list of C station for a second year. Fan welfare: Send four books to fans every week (each one has), and send various small gifts every month (gold boring enamelware cup, pillow, mouse pad, mug, etc.)

In this article we will discuss Lambda expressions in Java, which are Java’s foray into functional programming. It takes parameters and applies them to expressions or blocks of code. Here is a basic example of syntax:

(parameter1, parameter2) => expression
Copy the code

or

(parameter1, parameter2) => {code block}
Copy the code

Lambda expressions are very limited, and if it is not void, a value must be returned immediately. They can’t use keywords like if or for to keep things simple. If you need more lines of code, you can use code blocks instead.

Now you can’t just use expressions when implementing lambda expressions. Lambda is an implementation of a functional interface. A functional interface is an interface that has only one abstract method. The nice thing about lambdas is that they allow you to implement methods without having to implement classes and instantiated objects of interfaces. Here’s an example:

interface FuncInterface
{
    // Abstract function
    void abstractFun(int x);

    // Non-abstract (or default) functions
    default void normalFun(a)
    {
       System.out.println("Hello"); }}class Test
{
    public static void main(String args[])
    {
        // Implement a lambda expression for the above functional interface.
        AbstractFun () abstractFun()
        FuncInterface fobj = (int x)->System.out.println(2*x);

        // This calls the lambda expression above and prints 10.
        fobj.abstractFun(5); }}Copy the code

Lambda expressions are typically used as arguments to functions. For readability, you can also store lambda expressions in variables, as long as the type is an interface with a single method, the same number of arguments, and the same return type.

import java.util.ArrayList;
import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(1); Consumer<Integer> method = (n) -> { System.out.println(n); }; numbers.forEach( method ); }}Copy the code

A common use of lambda is to create threads. This is an example of implementing a Runnable object using a block of lambda code for thread execution.

// Runnable Lambda
Runnable task2 = () -> { System.out.println("Task #2 is running"); };

// Start the thread
new Thread(task2).start();
Copy the code

As beginners, most of us have been taught to program using OOP concepts, so using a different paradigm, such as functional programming, can be awkward. I’m still learning these concepts myself. Anyway I hope this article has taught you something. If you have more questions or want to delve deeper into this topic, feel free to comment or check out the resources below, where I’ve extracted code samples.

References:

www.w3schools.com/java/java_l… www.geeksforgeeks.org/lambda-expr… www.developer.com/microsoft/s… www.codejava.net/java-core/t…

Write it at the end

The author is determined to build a fishing site with 100 small games, update progress: 40/100

I’ve been writing tech blogs for a long time, mostly through Nuggets, and here’s my piece on how much you know about Lambda expressions in Java. I like to share technology and happiness through articles. You can visit my blog at juejin.cn/user/204034… For more information. Hope you like it! 😊

πŸ’Œ welcomes your comments and suggestions in the comments section! πŸ’Œ