Stream operations can be executed sequentially or in parallel to why.???
Flow
--java API new member ==> traversal data set advanced iterator
--Features: transparent parallel processing, without the need to write multithreaded code
-because operations such as filter, sorted, map, and collect are high-level artifacts that are not related to a specific threading model, their internal implementations can be single-threaded or transparent enough to take advantage of your multicore architecture! ==> in the book this sentence does not understand, the explanation is very vague !
Vegetable properties
Dish {
name;
Vegetarian;
calories;
type;
?
Dish (type) {
This . name;
This . Vegetarian;
This . calories;
This . type;
}
... getset ...
@Override
ToString () {
name;
}
Other}
}
Small example:
Stream {
Main (args) {
//Menu
List<DishArrays. Aslist (
Dish (Dish. Type. meat),
Dish (Dish. Type. meat),
Dish (Dish. Type. meat),
Dish (Dish. Type. Other ),
Dish (Dish. Type. Other ),
Dish (Dish. Type. Other ),
Dish (Dish. Type. Other ),
Dish (Dish. Type. FISH),
Dish (Dish. Type. FISH));
?
List<String//Data source
. //Set up Operation line
. Filter (x.//Remove the highest heat
. map (Dish:://Get the name of the dish
. Limit (3) //Select first three
. Collect (collectors. ToList ()); //Results exist in another list inside
System. Out . println (threehighcaloricdishnames);
}
}
map--accepts a lambda, transforms elements into other forms or extracts information
collect--converting a stream to another form
The difference between a stream and a collection
--a simple understanding of the ==> difference is calculated at the time
The collection ==> collection is an in-memory data structure that contains all the values currently in the data structure-each element in the collection must be counted before it can be added to the collection. (You can add or delete items to a collection, but whenever the elements in the collection are in memory, the elements have to be counted before they become part of the collection.)
Stream ==> flow is a conceptually fixed data structure (you cannot add or remove elements), its elements are calculated on demand , and from another point of view, the flow is like a collection of deferred creation: values are calculated only when required by the consumer (in the case of management, this is demand-driven , even in real-time manufacturing).
--for example, ==> collection is like when you watch a movie on a DVD, and the stream is to watch a movie online (eg: Iqiyi website)
--flow can only traverse once
Flow operations
Terminal actions: Operation to close a stream
Intermediate operation: The operation of a connected stream
List<Stringmenu
. Stream ()
. filter,{
System. Out . println ("filter"+x);
x.;
})
. map (x->{
System. Out . println ("map"+x.getName ());
x.getName ();
})
. Limit (3)
. Collect (collectors. ToList ());
System. Out . println (threehighcaloricdishnames);
Output:
Filterpork
Mappork
Filterbeef
Mapbeef
Filterchicken
Mapchicken
[Chicken]
?
Filter and map are two separate operations, but they merge into the same traversal ===> Loop Merge technology
Java8 the fourth Chapter