In some of my previous articles, I used lambda expression syntax, and some readers reported that the code didn’t make sense. The most important feature of Java 8 is lambda expressions. In fact, there are still a large number of programmers who do not use Java8 and who do not use lambda expressions. Therefore, it is necessary to write this article. If you think my article is helpful to you, I am looking forward to your attention.

Lambda expressions are the most popular and frequently used feature in Java 8. It introduces the concept of functional programming into Java. The advantage of functional programming is that it can help us save a lot of code. It is very convenient and easy to use, and can greatly improve our coding efficiency. In this article, we’ll show you what lambda expressions are and convert traditional Java code writing to lambda expression writing. You can see how lambda expressions simplify traditional code by using examples.

I. Interface definition

First of all, we need to understand what lambda expressions are saying. The answer is that lambda expressions express implementations of interface functions, so we need to do some preparatory work. In traditional development, we are not used to passing blocks of code to functions. All of our behavior definition code is encapsulated in the body of the method and executed by object reference, as with the following code:

Public class LambdaDemo {// function definition public void printSomething(String something) {system.out.println (something); } public static void main(String[] args) {LambdaDemo = new LambdaDemo(); String something = "I am learning Lambda"; demo.printSomething(something); }}Copy the code

You should be familiar with the development of the above code, which is a classic OOP implementation style. Let’s make a change to the above code, creating a functional interface and defining abstract methods for that interface.

Public class LambdaDemo {// interface Printer {void print(String val); } public void printSomething(String something, Printer Printer) {Printer. Print (something); }}Copy the code

Two, the traditional implementation of interface functions

In the above implementation, the Printer interface is responsible for the printing behavior, either console printing or other printing behavior. The method printSomething no longer defines the behavior, but executes the behavior defined by Printer, which makes the design more flexible. The code is as follows:

public static void main(String[] args) { LambdaDemo demo = new LambdaDemo(); String something = "I am using a Functional interface"; {@override public void print(String val) {// console print System.out.println(val); }}; demo.printSomething(something, printer); }Copy the code

We haven’t used lambda expressions yet. We just created a concrete implementation of the Printer interface and passed it to the printSomething method.

Iii. Implementation of lambda representation

More on the concept of lambda expressions later, let’s first learn the syntax of lambda expressions:

(Param1, param2, param3... , paramN) - > {// code block; }Copy the code
  • First we know that lambda expressions express interface functions
  • To the left of the arrow is the comma-separated list of formal arguments to the function
  • To the right of the arrow is the function body code

Now, let’s refactor the code in the first section using lambda expressions

public static void main(String[] args) { LambdaDemo demo = new LambdaDemo(); String something = "I am learning Lambda"; Printer Printer = (String toPrint)->{system.out.println (toPrint); }; // Call the interface to print demo.printSomething(something, printer); }Copy the code

Lambda expressions make our code much cleaner. There are actually more benefits to using lambda expressions in terms of performance and multi-core processing, but they only make sense once you understand the Java8 Streams API and are therefore outside the scope of this article (which was covered in previous articles).

Is the amount of code reduced compared to traditional Java code implementations? But it’s still not the easiest way to do it, so let’s do it step by step.

Printer printer = (String toPrint)->{System.out.println(toPrint); }; Printer Printer = (toPrint)->{system.out.println (toPrint); }; Printer = toPrint->{system.out.println (toPrint); }; Printer = toPrint-> system.out.println (toPrint);Copy the code

  • Even if the type of the parameter is not specified to the left of the arrow, the compiler will infer its type from the formal parameters of the interface method
  • When there is only one argument, we can omit the parentheses entirely
  • When the function body has only one line, we can omit the curly braces entirely

If our interface method definition does not take any arguments, we can replace it with empty parentheses:

() -> system.out.println ("anything you wan to print")Copy the code

So what does the code look like when we finally simplify the lambda expression? True features of Lushan Mountain:

public static void main(String[] args) { LambdaDemo demo = new LambdaDemo(); String something="I am Lambda"; PrintSomething (something, toPrint -> system.out.println (toPrint)); }Copy the code

We used lambda expressions to inline function call parameters, reducing the original nine lines of main method to just three. However, this is still not the ultimate code simplification that lambda expressions can achieve, and when you learn to use the Java8 Stream API in conjunction with lambda expressions, you will find that your coding efficiency will greatly improve!

conclusion

Lambda expressions represent interface functions, with function parameters to the left of the arrow and function body to the right of the arrow. Function parameter types and return value types can be omitted, and the program automatically determines the data type based on the context defined by the interface.

In this article, we took a thorough look at Lambda expressions in Java and learned how to use them to improve the efficiency and quality of interface implementation. Keep an eye out for more on this issue. The Stream API gives Lambda even more advantages when used in conjunction with the Collections framework.

We look forward to your attention

  • The blogger has recently written a new book: “SpringBoot Series – Chapter 16, Verse 97.”
  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.