vavr icon indicating copy to clipboard operation
vavr copied to clipboard

Seq.groupBy() preserving insertion order: guaranteed or accident of implementation?

Open jbrains opened this issue 3 years ago • 2 comments

I notice that Collections.groupBy() creates a LinkedHashMap, which preserves insertion order when classifying items by their new key. Is this intended to be guaranteed or is it merely an accident of implementation? If it is intended to be guaranteed, then I would like to add those details to the documentation and perhaps check whether any tests are needed to reflect this guarantee.

Mostly, I would like to assess my risk in assuming that the various groupBy() implementations will preserve insertion order in the future.

jbrains avatar Mar 20 '22 14:03 jbrains

By this I assume you mean Traversable.groupBy. Since this returns a Map, which doesn't necessarily guarantee order, I would assume that any apparent ordering is a coincidence. Furthermore, I would consider it a coincidence that it returns a LinkedHashMap because neither the method signature nor the documentation guarantee this. If the writer(s) of this method intended it to preserve order, then I expect they would have indicated so in the method signature by formally returning a LinkedHashMap.

dbear496 avatar Jul 17 '22 21:07 dbear496

Thanks for following up. Indeed, that's the one I mean.

I was left to conclude the same thing, but I wondered whether perhaps someone might see this and think, "Actually, we do want to preserve insertion order and maybe it should return a more-specific implementation of Map."

jbrains avatar Jul 18 '22 14:07 jbrains