Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jbrains opened this issue Mar 20, 2022 · 2 comments

Comments

@jbrains
Copy link
Contributor

jbrains commented Mar 20, 2022

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
Copy link
Contributor

dbear496 commented Jul 17, 2022

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
Copy link
Contributor Author

jbrains commented Jul 18, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants