Ask a question
How to use Java's lambda expressions and Stream? ? ?
Solve the problem
The syntax of Lambda expression
Basic syntax:
1 2 3 | [code](parameters) -> expression
或
(parameters) ->{ statements; }
|
Copy after login
Look at the examples to learn!
Example 1: Define an AyPerson class to prepare for subsequent testing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | [code]package com.evada.de;
import java.util.Arrays;
import java.util.List;
class AyPerson{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AyPerson(String id, String name) {
this.id = id;
this.name = name;
}
}
public class LambdaTest {
public static void main(String[] args) {
List<String> names = Arrays.asList( "Ay" , "Al" , "Xy" , "Xl" );
names.forEach((name) -> System.out.println(name + ";" ));
}
}
|
Copy after login
The above example names.forEach((name) -> System.out.println(name + “;”));
is similar to:
1 2 3 4 5 | function (name){
System.out.println(name + “;”);
}
|
Copy after login
Result: Example 2: Copy the following code into the main function above
1 2 3 4 5 | [code]List<Student> personList = new ArrayList<>();
personList.add( new Student( "00001" , "Ay" ));
personList.add( new Student( "00002" , "Al" ));
personList.add( new Student( "00003" , "To" ));
personList.forEach((person) -> System.out.println(person.getId()+ ":" + person.getName()));
|
Copy after login
Result:
1 2 3 | [code]00001:Ay
00002:Al
00003:To
|
Copy after login
Example 3: f**ilter( in Lambda and Stream classes )**Method
1 2 3 4 5 6 7 8 9 10 | [code]List<AyPerson> personList = new ArrayList<>();
personList.add( new AyPerson( "00001" , "Ay" ));
personList.add( new AyPerson( "00002" , "Al" ));
personList.add( new AyPerson( "00003" , "To" ));
personList.stream()
.filter((person) -> person.getId().equals( "00001" ))
.forEach((person) -> System.out.println(person.getId() + ":" + person.getName()));
|
Copy after login
Result:
Example 4: collect() method in Stream class,
1 2 3 4 5 6 7 8 9 10 11 | [code] List<AyPerson> personList = new ArrayList<>();
List<AyPerson> newPersonList = null;
personList.add( new AyPerson( "00001" , "Ay" ));
personList.add( new AyPerson( "00002" , "Al" ));
personList.add( new AyPerson( "00003" , "To" ));
newPersonList = personList.stream()
.filter((person) -> person.getId().equals( "00002" )).collect(Collectors.toList());
newPersonList.forEach((person) -> System.out.println(person.getId() + ":" + person.getName()));
|
Copy after login
##In addition to Put it in List, you can also put it in Set, etc...
Result:
There are many useful methods that can be used in Stream, such as count, limit, etc. Go to the API to learn by yourself
The above is the content of simple examples of Lambda expressions and Stream classes in Java. For more related content, please pay attention to the PHP Chinese website ( www.php.cn)!