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?
Map<String, List<String>> myMap1 = new HashMap<String, List<String>>();
List<String> citiesInUSA = new ArrayList<>();
citiesInUSA.add("New York");
citiesInUSA.add("Los Angeles");

List<String> citiesInUK = new ArrayList<>();
citiesInUK.add("London");
citiesInUK.add("Manchester");

myMap1.put("USA", citiesInUSA);
myMap1.put("UK", citiesInUK);


System.out.println(myMap1);
Map<String, List<String>> myMap1 = new HashMap<String, List<String>>();
List<String> citiesInUSA = new ArrayList<>();
citiesInUSA.add("New York");
citiesInUSA.add("Los Angeles");

List<String> citiesInUK = new ArrayList<>();
citiesInUK.add("London");
citiesInUK.add("Manchester");

myMap1.put("USA", citiesInUSA);
myMap1.put("UK", citiesInUK);


System.out.println(myMap1);
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
JavaBot
JavaBot•3mo ago
⌛ This post has been reserved for your question.
Hey @Fragmented friends! Please use /close or the Close Post button 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Madjosz
Madjosz•3mo ago
It provides a different API and encapsulates all the list handling under the hood for you.
bambizas19
bambizas19OP•3mo ago
Cna you explain in more detail? Because i dont really understand what problem it solves
JavaBot
JavaBot•3mo ago
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.
Madjosz
Madjosz•3mo ago
I'd like to cite the JavaDoc from Guava's Multimap here:
A collection that maps keys to values, similar to Map, 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 the Multimap API is based on the second form. So, using the multimap shown above as an example, the size() is 3, not 2, and the values() collection is [1, 2, 3], not [[1, 2], [3]]. For those times when the first style is more useful, use the multimap's asMap() view (or create a Map<K, Collection<V>> in the first place).
bambizas19
bambizas19OP•3mo ago
Wait, but why are you talking about multimap? Because i was talking about MultiValueMap from Spring
Madjosz
Madjosz•3mo ago
They are both basically the same functionality, just from another library.
bambizas19
bambizas19OP•3mo ago
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 😄
Madjosz
Madjosz•3mo ago
And the docs of Guava are more detailed than the Spring ones.
Kyo-chan
Kyo-chan•3mo ago
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
bambizas19
bambizas19OP•3mo ago
Im not really sure what you mean by that What it has to do with assembly?
Kyo-chan
Kyo-chan•3mo ago
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
ayylmao123xdd
ayylmao123xdd•3mo ago
lmao in short its a bit fancy way to do the same thing that u can just do with a normal map
bambizas19
bambizas19OP•3mo ago
Thanks for actually answering my question. When would you choose Map with List, and MultiValueMap?
JavaBot
JavaBot•3mo ago
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.
ayylmao123xdd
ayylmao123xdd•3mo ago
personally i would never pick multivalue map cuz like whats the point
bambizas19
bambizas19OP•3mo ago
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
ayylmao123xdd
ayylmao123xdd•3mo ago
yes addall method but with a normal map its gonna be a one liner too if you do put(key, list of french cities)
bambizas19
bambizas19OP•3mo ago
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"
Kyo-chan
Kyo-chan•3mo ago
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
bambizas19
bambizas19OP•3mo ago
So at the end of the day MultiValueMap is just a fancier Map. Both do the same thing, but MultiValueMap just has fancier syntax?
Kyo-chan
Kyo-chan•3mo ago
fancy doesn't quite cut it. Clearer, shorter, more expressive, more to the point.
bambizas19
bambizas19OP•3mo ago
Hm. But thats just a personal preference, right?
Kyo-chan
Kyo-chan•3mo ago
No. Facts are what they are. With more expressiveness comes less bugs and less maintenance costs
bambizas19
bambizas19OP•3mo ago
can you give an example? Lets say I want to add new French city. How would it look in both scenarios?
Kyo-chan
Kyo-chan•3mo ago
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
bambizas19
bambizas19OP•3mo ago
You didnt answer my question. Thats how you can add new country with its cities:
Map<String, List<String>> myMap1 = new HashMap<String, List<String>>();
List<String> citiesInUSA = new ArrayList<>();
citiesInUSA.add("New York");
citiesInUSA.add("Los Angeles");

List<String> citiesInUK = new ArrayList<>();
citiesInUK.add("London");
citiesInUK.add("Manchester");

myMap1.put("USA", citiesInUSA);
myMap1.put("UK", citiesInUK);
myMap1.put("France", Arrays.asList("Paris", "Lille"));

System.out.println(myMap1);
Map<String, List<String>> myMap1 = new HashMap<String, List<String>>();
List<String> citiesInUSA = new ArrayList<>();
citiesInUSA.add("New York");
citiesInUSA.add("Los Angeles");

List<String> citiesInUK = new ArrayList<>();
citiesInUK.add("London");
citiesInUK.add("Manchester");

myMap1.put("USA", citiesInUSA);
myMap1.put("UK", citiesInUK);
myMap1.put("France", Arrays.asList("Paris", "Lille"));

System.out.println(myMap1);
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"));?
Kyo-chan
Kyo-chan•3mo ago
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
bambizas19
bambizas19OP•3mo ago
Answer my question
Kyo-chan
Kyo-chan•3mo ago
Pay me and I will within 48h
bambizas19
bambizas19OP•3mo ago
Thank you for your time. Goodbye
JavaBot
JavaBot•3mo ago
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.
tjoener
tjoener•3mo ago
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
bambizas19
bambizas19OP•3mo ago
is used to convert an array into a fixed-size list
bambizas19
bambizas19OP•3mo ago
sounds good to me
tjoener
tjoener•3mo ago
yeah, try adding a city to a country... Also if you want to multimap properly, use map.computeIfAbsent(key, _ -> new ArrayList<>()).add(value)
JavaBot
JavaBot•3mo ago
💤 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.

Did you find this page helpful?