Open jbrains opened 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
.
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
."
I notice that
Collections.groupBy()
creates aLinkedHashMap
, 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.