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

Time:07-07

I'm trying to convert a list of objects into a list of lists of objects, i.e. 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();

CodePudding user response:

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.

  • Related