简体   繁体   中英

Java Streams - How to preserve the initial Order of elements while using Collector groupingBy()

I'm trying to convert a list of objects into a list of lists of objects, ie List<T> into List<List<T>> and group them by a list of strings used as sorting keys .

The problem is that after the groupingBy() , the return value is a map, and that map a different order of values.

What can I do to preserve the initial order of elements in the list groupedTreesList ?

My code:

Map<List<String>,List<QueryTreeProxy>> groupedMap = treesProxyList.stream()
    .collect(Collectors.groupingBy(
        QueryTreeProxy::getMappingKeys,
        Collectors.toList()
    ));

List<List<QueryTreeProxy>> groupedTreesList = groupedMap.values().stream().toList();

groupingBy() , the return value is a map and that map output a different order of values list

You can use another flavor of groupingBy(classifier, mapFactory, downstream) , which allows you to specify the type of the Map. And we can use LinkedHashMap to preserve the initial order.

And there's no need to generate the second stream, you can use parameterized contractor of ArrayList instead.

Map<List<String>, List<QueryTreeProxy>> groupedMap = treesProxyList.stream()
    .collect(Collectors.groupingBy(
        QueryTreeProxy::getMappingKeys,
        LinkedHashMap::new,
        Collectors.toList()
    ));
    
List<List<QueryTreeProxy>> groupedTreesList = new ArrayList<>(groupedMap.values());

Besides that, using a mutable object as a key isn't a good practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM