Why are there no `groupingBy`s for primitive arrays?

Why are there no groupingBy extension functions for primitive arrays?

1 Like

I have no context or insight into the actual rational behind the Kotlin dev team, but two things jump out to me about this problem space with specifically groupBy logic.

  1. I would imagine a big contributor to the lack of standard library support in Kotlin for this comes from the lack of support for this in standard Java libraries. There are plenty of 3rd party libraries that approach this problem, but Kotlin has intentionally in the past only implemented ‘reasonable and simple’ extensions of core Java libraries.
  2. There is a lot of hidden complexity in the variety of uses for grouping logic. For other collection operations both the computational and memory complexity tend to be fairly straight forward and linear in nature. Comparatively grouping requires a new memory footprint as well as potentially multiple lambda logic for the grouping and mapping steps. This assumes that and standard implementation of grouping would be able to support a variety of approaches to combining the key resolution and list reduction problems.

These two issues would be challenging enough, but throw in the complexity of trying to support coroutines or other performance specific features and I can entirely understand why this isn’t a priority for the relatively small Kotlin dev team.


1 Like

There is: groupBy - Kotlin Programming Language

inline fun <K> IntArray.groupBy(
    keySelector: (Int) -> K
): Map<K, List<Int>>

There are groupBys but there are no groupingBys.

1 Like

Sry, you are right. I mixed up the functions.

When designing groupingBy operation we haven’t found an evidence that providing it for primitive arrays would be a significant gain. See the decision about the operation receivers in the corresponding KEEP-23.

The intended workaround – wrapping a primitive array into a list with asList() and calling groupingBy on that list – isn’t much longer and offers the same performance.

1 Like