Use of data flow (stream)
1) Stream's acquisition
① through the collection collection get
1
list<integer> list = new Arraylist<integer> (arrays.aslist (1,2,3,4,5));
2
stream<integer> stream = List.stream ();
② is obtained by array
1
String[] Array = {"Are", "You", "OK"};
2
stream<string> stream = Array.stream (Array);
3
4
Handling of primitive type arrays
5
Int[] array = {1,2,3,4,5};
6
stream<integer> stream = Arrays.stream (array). boxed (); At this point Arrays.stream (array) gets a Intstream object
③ by value (as far as possible using the above 2 ways to get, this way there are defects)
1
stream<string> stream = Stream.of ("Are", "You", "OK");
2) Filtering filter
The filter function receives a lambda expression as a parameter that returns a Boolean, in which the stream transports the element to filter one at a time and filters out the element that executes the result to true;
1
Filter out non-empty items in a list
2
list<string> list = Arrays.aslist ("Are", "You", "", "OK");
3
list<string> filted = List.stream ()
4
. Filter (x->!x.empty ())
5
. Collect (Collectors.tolist ());
3) Go to heavy distinct
1
list<string> list = Arrays.aslist ("Are", "You", "You", "OK");
2
list<string> distincted = List.stream ()
3
. Distinct ()
4
. Collect (Collectors.tolist ());
4) Interception of limit
The first n elements of an intercept stream: