Sorting problems after Collectors.groupingby grouping

Source: Internet
Author: User
Tags wrapper

A hashmap is generated in the default Groupingby code (HashMap is unordered, the put order is inconsistent with the order of Get)

    • HashMap is unordered, HashMap in put is based on the key hashcode hash and then put into the corresponding place. So in a certain order put into the HashMap, and then traverse out the order of HashMap and the order of the put is different (unless the key has been put in accordance with the Hashcode sort number, the probability is very small)
    • Simple HashMap is not possible to achieve the sorting, this sort refers to, we put the key value pairs in a certain order put into the hashmap, and then in the operation of the key value pair, it is to put in the order of the key values are taken out.
    • Java has provided linkedhashmap after JDK1.4 to help us achieve an orderly hashmap! Linkedhashmap The key value pairs are taken in the order you put them.

This creates a list<model> if it is ordered, the order of the Model after Groupingby is not controllable.

Now it's a scene like this

In the CMS, the modules of each page are emitted sequentially, and the contents of each module are sequentially

List<model> list=arrays.aslist (M1,M2,M3)

Now you need to group the elements inside, but the order after grouping must also be m1,m2,m3 ... The middle can be missing, but not disorderly.

The following are legal m1,m3 or m2,m3 but not m3,m2

If the order of the following code list is id=2 before id=1, the access after grouping must be id=2 before the

However, if you call the default grouping, you will find that the id=1 is in front ( in the back will be in front; before it will be behind)

Output is always

1

[A12,A11]

2

[A2,a21]

But the expected output is

2 [A21,A2] 1 [A12,A11]

You cannot use the default method if you need to maintain sorting, and you must use the annotated method (explicitly using Linkedhashmap to keep the order).

The following is a description of the Groupingby parameter

You can see that there are three parameters, the first parameter is the key Function , the second parameter is a map factory, which is the final result of the container, the general default is adopted HashMap::new , the last parameter is very important is a downstream , type is Collector , is also a collector, that is to say, These three parameters are actually to solve the problem of grouping

    1. First parameter: Grouping by what category

    2. Second parameter: What container to use to save the last return

    3. Third parameter: After sorting by the first parameter, how the result of the corresponding classification is collected

In fact, a parameter of the Collectors.groupingBy method, the second parameter is the default HashMap::new , the third parameter collector is actually the defaultCollectors.toList

Sorting problems after Collectors.groupingby grouping

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.