vavr-io / vavr

vʌvr (formerly called Javaslang) is a non-commercial, non-profit object-functional library that runs with Java 8+. It aims to reduce the lines of code and increase code quality.
https://vavr.io
Other
5.68k stars 631 forks source link

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

Open jbrains opened 2 years ago

jbrains commented 2 years ago

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.

dbear496 commented 2 years ago

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.

jbrains commented 2 years ago

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."