Java8 in the Steram represents only the data flow, and InputStream , OutputStream is a completely different concept. The stream Here is an enhancement to the collection (collection) function, focusing more on the convenience, efficient aggregation, and high volume data manipulation of the collection objects.
Collec () function
will be Stream The returned elements are spelled ArrayList . The parameter is collectros interface , which provides great convenience
<r, a> R collect (collector<? super T,a, r> Collector);
Parsing: A function is a generic method
parameter is a generic constraint and can only be a parent class of a type
parameter colector interface
except toList (), Toset () , Tomap () , there are joing () stitching strings, Groupingby and Other methods
Colector method Groupingby Basic Grouping
use: we use Groupingby methods to group people by age
Map (integer,list<person>) Personage=people.stream (). Collect (Collectros.groupingby (person::getage));
Person::getage returns the value of a property for a method reference. This looks like the horizontal C + + function scope. In fact Groupingby () accepts a lambda expression
Collect (Collectors.groupingby (p->p.getage))
Gruop Pingby There are multiple overloads, one of which is a collector, and the grouped data is collected by the specified criteria
Groupingby selecting Groups
Use: We will group the data, only get the name of it
Map (integer,list<string> nameofage=people.stream (). Collect (Groupingby (person::getage,mapping (Person:: Getname,tolist ()))); System.out.println ("People grouped by age:" + nameofpeoplebyage);
Resolution: two parameters: the first is the age, grouping conditions, the second is the collection period,themapping function returns the result,mapping even a parameter, The properties of the mappings and the places to be collected, such as list or set
The result of the output is
People grouped byage: {35=[greg], 20=[john], 21=[sara, Jane]}
Summary:
by looking at the source code we found Groupingby the parameter is function<t,r> function interface, the operation of the function is to receive parameters as T, return R type of operation, which is plainly a func the generic delegate with the parameter T , return to T , and NET in the func is an effect that simplifies the writing of code to a great extent
Stream of new methods of Java8 (i)