Whats the point of Spring's MultiValueMap?
Hello everyone. I came across Spring's MultiValueMap and started wondering whats the point of it? How is it better than old school way?
Instead of List, u can use collection, if u want to add new city, you just add it to the list. Gemini seems to be too stupid to understand my point. So id like to ask real people haha 😄
39 Replies
⌛ This post has been reserved for your question.
Hey @Fragmented friends! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
It provides a different API and encapsulates all the list handling under the hood for you.
Cna you explain in more detail?
Because i dont really understand what problem it solves
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
I'd like to cite the JavaDoc from Guava's
Multimap here:
A collection that maps keys to values, similar toMap, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values: a → 1, 2 b → 3 ... or as a single "flattened" collection of key-value pairs: a → 1 a → 2 b → 3 Important: although the first interpretation resembles how most multimaps are implemented, the design of theMultimapAPI is based on the second form. So, using the multimap shown above as an example, thesize()is 3, not 2, and thevalues()collection is[1, 2, 3], not[[1, 2], [3]]. For those times when the first style is more useful, use the multimap'sasMap()view (or create aMap<K, Collection<V>>in the first place).
Wait, but why are you talking about multimap?
Because i was talking about
MultiValueMap from SpringMultiValueMap (Spring Framework 6.2.11 API)
declaration: package: org.springframework.util, interface: MultiValueMap
They are both basically the same functionality, just from another library.
Oh, okay.
you wrote
each key may be associated with multiple values. But in my question i showed, that i can do the same thing with ordinary Map. So the question is the same 😄And the docs of Guava are more detailed than the Spring ones.
You could also program in assembly rather than in Java, and you could just do the work by hand instead of using computers, all these things can totally be done.
The point of these classes is not to make possible was used to be impossible. It is to handle for you some of the work that you shouldn't need to do yourself when what you want to work with is a map with which keys can be mapped to multiple values
Im not really sure what you mean by that
What it has to do with assembly?
Well, put a note to come back reading it back every month, as you gain experience. As you progress, you will understand
You said "I can do these things with a normal Map too"
But such words are actually not a point, it cannot possibly be a point.
If you want to consider such things, then it is also true that rather than using a programming language, you could use a processor language instead.
Which is a ridiculous and should help you realise that was a silly thing to say
lmao
in short its a bit fancy way to do the same thing that u can just do with a normal map
Thanks for actually answering my question. When would you choose Map with List, and MultiValueMap?
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
personally i would never pick multivalue map
cuz like whats the point
As i understand, if i wanted to add French cities, it would be a one liner with MultiValueMap? Instead of a couple of lines with ordinary Map
yes
addall method
but with a normal map its gonna be a one liner too
if you do
put(key, list of french cities)
Damn. So it seems like just people are reinventing the wheel just for the sake of it 😄
"Just because you can, doesnt mean you should"
Nah, this guy is a little bit of bad faith. His way is dedicated solely to when it is important to have a map of collections, rather than a map to multiple values
Now, it's probably not worth it to import a dependency just to have the multimap. But if you already have spring, then the multimap being already there justifies the easier use
So at the end of the day MultiValueMap is just a fancier Map. Both do the same thing, but MultiValueMap just has fancier syntax?
fancy doesn't quite cut it. Clearer, shorter, more expressive, more to the point.
Hm. But thats just a personal preference, right?
No. Facts are what they are. With more expressiveness comes less bugs and less maintenance costs
can you give an example? Lets say I want to add new French city. How would it look in both scenarios?
Your example is weird. To add a French city you'll use a List, or a Set, or a queue or a stack. Not a map
You didnt answer my question. Thats how you can add new country with its cities:
So please answer my question:
Why would one use MultiValueMap, when one can use Map to add new key and new values in such way:
myMap1.put("France", Arrays.asList("Paris", "Lille"));?I already pointed out that the idea was never to make possible what used to have been impossible
As for an example, I would have done it 30 mins ago, but I don't have time anymore now. I hope someone else will. But I wouldn't trust that other guy. Not particularlt honest
Answer my question
Pay me and I will within 48h
Thank you for your time. Goodbye
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.Arrays.asList does not what you think it does
I would use a multivaluemap when I have one
I will do it manually when I don't and I only need it in one or two places
is used to convert an array into a fixed-size listsounds good to me
yeah, try adding a city to a country...
Also if you want to multimap properly, use
map.computeIfAbsent(key, _ -> new ArrayList<>()).add(value)💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.