Have a nice smile
Ask questions
How does a Java lambda expression and stream work???
solve the problem
The syntax of a lambda expression
Basic syntax:
(parameters) -> expression或(parameters) ->{ statements; }
Read the example study!
Example one: Define a Ayperson class to prepare for subsequent tests.
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; }}/** * Created by Ay on 2016/5/9. */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 + ";")); }}
The above example Names.foreach (name), SYSTEM.OUT.PRINTLN (name + ";"));
Similar to:
function (name) {//name is a parameter
SYSTEM.OUT.PRINTLN (name + ";"); /Method Body
}
Results:
Ay;Al;Xy;Xl;
Example two: Copy the following code into the main function above
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()));
Results:
00001:Ay00002:Al00003:To
Example three: F**ilter () * * method in Lambda and stream classes
List<AyPerson> personList = new ArrayList<>(); personList.add(new AyPerson("00001","Ay")); personList.add(new AyPerson("00002","Al")); personList.add(new AyPerson("00003", "To")); //stream类中的filter方法 personList.stream() //过滤集合中person的id为00001 .filter((person) -> person.getId().equals("00001")) //将过滤后的结果循环打印出来 .forEach((person) -> System.out.println(person.getId() + ":" + person.getName()));
Results:
00001:Ay
Example four: the collect () method in the Stream class,
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")); //将过滤后的结果返回到一个新的List中 newPersonList = personList.stream() .filter((person) -> person.getId().equals("00002")).collect(Collectors.toList()); //打印结果集 newPersonList.forEach((person) -> System.out.println(person.getId() + ":" + person.getName()));
In addition to the list can be placed in the set and so on ...
Results:
00002:Al
There are many useful ways to use the stream, such as count,limit and so on, you can learn from the API.
Reading Comprehension
From the tomb of the Fireflies
- Cherish today, cherish now, who knows tomorrow and the accident, which one comes first.
- Why are fireflies dying so fast?
- Sister died before the said: "Good delicious oh, thank you brother!" "No complaints, no pain." With a smile, with satisfaction to leave the cruel world ~ ~
other
If there is a little bit of happiness for you, let happiness continue to pass, welcome reprint, Praise, top, welcome to leave valuable comments, thank you for your support!
A simple example of the lambda expression and stream class in Java