Stream only represents the data Stream, and there is no data structure, so it can no longer be traversed after it is traversed once (this should be noted during programming, unlike Collection, number of times there is data in the traversal), its source can be Collection, array, io, and so on.
Stream provides a big data operation interface to make data operations easier and faster. It has filtering, ing, and reducing the number of traversal methods. These methods are divided into two types: the intermediate method and the terminal method. The "stream" abstraction should be inherently continuous, the intermediate method always returns Stream. Therefore, if we want to obtain the final result, we must use the endpoint operation to collect the final result of abortion. The difference between the two methods is to look at his return value. If it is Stream, it is an intermediate method; otherwise it is an endpoint method.
Filter
Filtering in data streams is the most natural operation we can think of first. The Stream interface exposes a filter method, which can accept the Predicate implementation that represents the operation to use a lambda expression that defines the filter condition.
Import java. util. stream. stream; public class StreamDemo {public static void main (String [] args) {List <User> users = new ArrayList <User> (); users. add (new User (20, "James"); users. add (new User (22, ""); users. add (new User (10, "Wang Wu"); Stream <User> stream = users. stream (); stream. filter (p-> p. getAge ()> 20); // filter those older than 20 }}
Map
If we filter some data, for example, when converting an object. The Map operation allows us to execute a Function implementation (Function <T, R> generic T, R represents the execution input and execution result respectively). It accepts input parameters and returns them.
Package com. yztcedu. lambdademo_01; import java. util. arrayList; import java. util. list; import java. util. stream. stream; public class StreamDemo {public static void main (String [] args) {List <User> users = new ArrayList <User> (); users. add (new User (20, "James"); users. add (new User (22, ""); users. add (new User (10, "Wang Wu"); Stream <User> stream = users. stream (); // all User objects older than 20 years old are converted to string 50 objects. Currently, the stream only contains string objects. Stream. filter (User user)-> user. getAge ()> 20). map (User user)-> {return "50 ";});}}
Count
The count method is the end method of a stream. It can make the final statistics of the stream results and return the long
Package com. yztcedu. lambdademo_01; import java. util. arrayList; import java. util. list; import java. util. stream. collector; import java. util. stream. stream; public class StreamDemo {public static void main (String [] args) {List <User> users = new ArrayList <User> (); users. add (new User (20, "James"); users. add (new User (22, ""); users. add (new User (10, "Wang Wu"); Stream <User> stream = users. stream (); long count = stream. Filter (User user)-> user. getAge ()> = 20 ). map (User user)-> {return "50 ";}). count (); // returns the number of elements in the stream. System. out. println (count );}}