Stream of new methods of Java8 (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.