Detailed explanation of Lambda and Stream in Java8 (with code)
This article brings you a detailed explanation of Lambda and Stream in Java8 (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Preface
This article mainly introduces the two main new features of Java8, lambda expression and Stream API. The two provide a higher level of abstraction and simplify development. ,Increase productivity.
2. Lambda expression
2.1 First introduction to Lambda expression
Create a thread and use an Runnable
anonymous The internal class
Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println("Hello Aron."); } });
may not seem like a big problem, but in fact the disadvantages are quite obvious: there are too many template syntaxes, and the only statements that really have business significance are System.out.println("Hello Aron.")
, because of this, also seriously interferes with our reading of the code.
After introducing lambda expression, you can write like this
Thread thread = new Thread(() -> System.out.println("Hello Aron."));
It’s too concise, is there any idea?
2.2 More Lambda expressions
Runnable runnable = () -> System.out.println("Hello World."); Consumer<T> tConsumer = bean -> System.out.println("Hello World."); Runnable runnable1 = () -> { System.out.println("Hello World."); System.out.println("Hello World."); };
The syntax is divided into 3 sections: parameters, ->
and statements, that is, (...)- > { ...}
2.3 Function interface
Java is a strongly typed language, and method parameters have fixed types. So here’s the problem. Lambda expressions, if regarded as a bunch of code fragments, will also express a clear intention. This intention can be understood as a functional interface for the time being.
In the process of programming, you will always encounter many functional interfaces. The following are some of the most important functional interfaces in JDK
Examples of interface parameter return types
Interface | Parameters | Return type | Example |
---|---|---|---|
boolean | Is the value equal to "Hello"? | ||
T | void | Output a value | |
T | R | Get a property of the object | |
None | T | Factory Method | |
T | T | Logical NOT (!) | |
(T, T) | T | Find the sum of 2 numbers ( ) |
The above is the detailed content of Detailed explanation of Lambda and Stream in Java8 (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In C++, there are two ways to handle exceptions using Lambda expressions: catch the exception using a try-catch block, and handle or rethrow the exception in the catch block. Using a wrapper function of type std::function, its try_emplace method can catch exceptions in Lambda expressions.

In C++, a closure is a lambda expression that can access external variables. To create a closure, capture the outer variable in the lambda expression. Closures provide advantages such as reusability, information hiding, and delayed evaluation. They are useful in real-world situations such as event handlers, where the closure can still access the outer variables even if they are destroyed.

Java8 calculates the date one year ago or one year later using the minus() method to calculate the date one year ago packagecom.shxt.demo02;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;publicclassDemo09{publicstaticvoidmain(String[]args ){LocalDatetoday=LocalDate.now();LocalDatepreviousYear=today.minus(1,ChronoUni

The advantages of lambda expressions in C++ multi-threaded programming include simplicity, flexibility, ease of parameter passing, and parallelism. Practical case: Use lambda expressions to create multi-threads and print thread IDs in different threads, demonstrating the simplicity and ease of use of this method.

C++ Lambda expressions support closures, which save function scope variables and make them accessible to functions. The syntax is [capture-list](parameters)->return-type{function-body}. capture-list defines the variables to capture. You can use [=] to capture all local variables by value, [&] to capture all local variables by reference, or [variable1, variable2,...] to capture specific variables. Lambda expressions can only access captured variables but cannot modify the original value.

Stream operation is a highlight of Java8! Although java.util.stream is very powerful, there are still many developers who rarely use it in actual work. One of the most complained reasons is that it is difficult to debug. This was indeed the case at the beginning, because streaming operations such as stream cannot be used in DEBUG When it is one line of code, when it comes to the next step, many operations are actually passed at once, so it is difficult for us to judge which line in it is the problem. Plug-in: JavaStreamDebugger If the IDEA version you are using is relatively new, this plug-in is already included and does not need to be installed. If it is not installed yet, install it manually and then continue below.

There are three ways to capture lambda expressions of external variables in C++: Capture by value: Create a copy of the variable. Capture by reference: Get a variable reference. Capture by value and reference simultaneously: Allows capturing of multiple variables, either by value or by reference.

java8's stream takes maxpublicstaticvoidmain(String[]args){Listlist=Arrays.asList(1,2,3,4,5,6);Integermax=list.stream().max((a,b)->{if (a>b){return1;}elsereturn-1;}).get();System.out.println(max);}Note: The size is determined here through positive and negative numbers and 0 values. Instead of writing it directly if(a>b){returna;}elseretur
