java8 tips to ensure the same sort grouped groupingBy

Question:
After experiencing a small problem, check out a set of data, according to which the property will be groupBy grouping, grouped order to ensure the same. However, the actual packet with groupBy, the returned data is haphazard, not returned in the order of the original list

Investigation
first looks to solve api in view of java.util.stream package Collectors Java class groupingBy way to achieve the following results:

//一个参数
   public static <T, K> Collector<T, ?, Map<K, List<T>>>
    groupingBy(Function<? super T, ? extends K> classifier) {
        return groupingBy(classifier, toList());
    }

//两个参数
    public static <T, K, A, D>
    Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
                                          Collector<? super T, A, D> downstream) {
        return groupingBy(classifier, HashMap::new, downstream);
    }
    
//三个参数
  public static <T, K, D, A, M extends Map<K, D>>
    Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
                                  Supplier<M> mapFactory,
                                  Collector<? super T, A, D> downstream) {......}

GroupingBy found by java api calls are within their own created a HashMap (HashMap :: new). Because hashMap 'is no disorder, it is the hash key according to hashCode, and then placed in the corresponding place. So put in a certain order into a HashMap, then traverse the HashMap with a sequence of different order put.

I know this'll understand why the disorder. So we directly call the three parameters groupingBy method mapFactory, passed in sequential Map, LinkedHashMap it.

Information on their own Baidu LinkedHashMap

Resolve
to create a Person data collection, and by age sorting, grouping after ordering, guaranteed not to change the order after grouping.

//创建数据
 private static List<Person> getPersionList() {
        List<Person> persons = new ArrayList<>();
        for (int i = 1; i <= 40; i++) {
            Random r = new Random();
            Person person = new Person();
            person.setName("abel-" + i);
            person.setSex((int) (Math.random() * 2));
            person.setGroup(String.valueOf(i%2));
            person.setAge(25 + r.nextInt(50));
            persons.add(person);
        }
        return persons;

    }


    / **
     * packet
     * /
    Private static void groupByTest () {
        List <the Person> persons getPersionList = ();
        // sort the list, and ordered grouping results sorted according
        LinkedHashMap <Integer, List <Person >> ageMap personsSort.stream = () the sorted (Comparator.comparingInt (the Person :: getAge)) the collect (Collectors.groupingBy (getAge the Person ::, :: a LinkedHashMap new new, Collectors.toList ()));..
    }

About sorting Comparator.comparingInt reference:
https://www.jianshu.com/p/3f621e51f3
 

Published 776 original articles · won praise 50 · Views 150,000 +

Guess you like

Origin blog.csdn.net/weixin_44018338/article/details/104982582