Hello, World! Is it still a fact that there is no...
# stdlib
b
Hello, World! Is it still a fact that there is no MultiMap in the stdlib?
g
There is no MultiMap in stdlib
b
too bad 🙂 Thanks!
g
do you need one for MPP?
b
I'm on Android actually
g
You can use com.google.common.collect.Multimap
b
yeahhh it's just too bad it's not directly in Kotlin imho - don't really want to add a dependency just for this little bit of code I'm working on right now
in the end, I'll just code it myself, but I feel thousands of devs are re-implementing the wheel on all their projects 🙂
g
so you don’t want to add library to you project because it’s unnecessary, but want to add a new class to stdlib, so every user in world will have it?
😂 1
💥 1
re-implementing the wheel on all their projects
Or use many existing ones, Guava probably the most common dependency
One thing with stdlib, that it has very limited own collections, because it uses Java implementations on runtime, so it allows to keep stdlib quite small
b
Well of course everything is a compromise, but given that IMO the vast majority of projects will at some point need a Multimap, and its implementation is probably quite modest, yes this addition is worth it. Also as you noted Java libraries are OK, but only for JVM or Android. A stdlib solution would work everywhere. And also Java solutions are probably not very good with nullability.
g
Yes, it always trade off
👍 1
r
given that IMO the vast majority of projects will at some point need a Multimap
IMO this is an extreme over estimation, but that may just be selection bias.
👆 2
b
😄 yeah I mean, if you wait long enough, the probability for a project to need it will tend to 1
c
I don't know that I've had a reason to use a MultiMap. Honestly this seems like it could make for an advanced collections library that someone could in theory open source and give back to the community.
b
Another thing to consider is that since it's so easy to implement, most people will prefer to just do it themselves than add a dependency (especially on mobile where adding a dependency is a big deal). But then the situation is not ideal (the everybody re invents the wheel part).
c
Honestly whenever I've needed a Map that requires multiple values for a key I just do
Map<K, List<V>>
which isn't that much work. It looks like iterating over the maps is the same amount of effort
b
exactly, that's a multimap 🙂 You also have to deal with the empty list vs no element for the key. Essentially you're implementing it yourself, when such a prevalent and simple utility could be in the stdlib
... I mean for instance,
shuffle
has just been added to arrays. Don't get me wrong, that's cool! But is it really used that often? I don't know if there's an easy standard to decide what should be in the stdlib vs what should be kept in external libs. Probably a very hard question.
d
You could use a
typealias
and a few extension methods to effectively turn any
Map<K, MutableCollection<V>>
into a
MultiMap<K, V>
, just slightly less performant than Guava's implementation. Same deal for
Map<E, Int>
into a
MultiSet<E>
.
c
I use shuffle a ton for unit testing
g
I mean for instance, 
shuffle
 has just been added to arrays
Shuffle is just a small extension function, it was added recently because stdlib Random is introduced
b
@Derek Peirce I mean yes, part of my point is that it's easy/small to implement indeed 🙂 More reason for it to be there already once and for all. I mean
let
is also easy to implement but we wouldn't like to do it ourselves in every project.
g
Special collection type is not so simple as let
if you have a few extension function to simplify work with Map<K, List<V>>, be free to suggest it for stdlib
b
I guess. The chances of it being accepted seem low however judging by this thread 🙂 But I may try, it could be a fun exercise in any case.
g
because it depends on what will be suggested
👍 1
because idea to add a special collection for it (and actually multiple collections) doesn’t look reasonsable