java8 of groupingby

Many people especially fresh graduates graduating students how many have learned something a little java8, knowing that there are a lot of new features

But in practicality And I have no place to use. . Before Before Internship also learned some things, but not used as soon forget the light

These days a lot of use in the project, was able in retrospect:

The major said groupingby, often in the project because of problems query efficiency and batch query some of the DO, but after a batch query

Is the need to classify the query to the list, such as a DO class person, which includes age, gender and other properties, depending on the need at this time

Ages on this list is filtered according to the previous method, only the constant need to own a new list or a manual on the map which filter out the age,

But using groupingby in java8 which is very convenient, for example, you need only filter based on age

// based on the id batch query list

List<Person> list = getByPersonId(List<String> ids) 

Map<int,List<Person>> map = list.stream.collect(Collectors.groupingby(Person::getAge))

Map thus obtained is filtered according to the age key map where a good map of the age, value is good according to age filter list

 

Guess you like

Origin www.cnblogs.com/qtlq/p/11357475.html