Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiValueMap in java

I'm studying with Hashmap with Multiple parameters(1 key, 2 values) and i was able to find apache multiValueMap for my issue.

Here is my codes for multiValueMap.

import java.util.Set;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.collections.MultiMap;

public class multiValueMap {

public static void main(String args[]) {
   String a, b, c;
   MultiMap mMap = new MultiValueMap();

   mMap.put("a", "Hello there, It's a wonderful day");
   mMap.put("a", "nice to meet you");

   Set<String> keys = mMap.keySet();

   for (String key : keys) {
      System.out.println("Key = " + key);
      System.out.println("Values = " + mMap.get(key));
      a = String.valueOf(mMap.get(key));

      System.out.println("A : " + a);
    }
 }
}
// The result as below
 Key = a 
 Value = [Hello there, It's a wonderful day, nice to meet you]
 A : [Hello there, It's a wonderful day, nice to meet you]

Here is my question how can I store first value for string b, and second for c? if I substring the MultiMap values depends on "," then it would stores Hello there only. please give me helpful your advices.

like image 916
user3810857 Avatar asked Jun 08 '15 08:06

user3810857


People also ask

How do I create a MultiValueMap?

Method SummaryAdd the given single value to the current list of values for the given key. Add all the values of the given list to the current list of values for the given key. Add all the values of the given MultiValueMap to the current values. Add the given value, only when the map does not contain the given key.

How do I create a multivalued Map in Java?

Method SummaryAdd multiple values to the current list of values for the supplied key. Add a value to the first position in the current list of values for the supplied key. Compare the specified map with this map for equality modulo the order of values for each key.

How do you send a MultiValueMap in Postman?

When you send the request via POSTMAN tool, select the type of HTTP method (POST, PUT, DELETE), then select the "raw" option in "Body" tab and then just add the JSON of the Map in it. Make sure you have selected "Content-type" as "application/json" in "Headers" .

How do I store multiple values in a HashMap?

Below is the syntax for inserting the values into HashMap. HashMap<String, Integer> hm = new HashMap<String, Integer>(); hm. put("one", 100); hm. put("two", 200); hm.


2 Answers

You can try following :

String a, b, c;

MultiMap mMap = new MultiValueMap();
mMap.put("a", "Hello there, It's a wonderful day");
mMap.put("a", "nice to meet you");

Set<String> keys = mMap.keySet();

for (String key : keys) {
    System.out.println("Key = " + key);
    System.out.println("Values = " + mMap.get(key));
    List<String> list = (List<String>) mMap.get(key);

    b = list.get(0);
    c = list.get(1);
    System.out.println("B : " + b);
    System.out.println("C : " + c);
} 
like image 189
Sachin Gupta Avatar answered Sep 29 '22 02:09

Sachin Gupta


You don't have to do a split. This is the documentation of MultiMap that is found:

MultiMap mhm = new MultiHashMap();
 mhm.put(key, "A");
 mhm.put(key, "B");
 mhm.put(key, "C");
 Collection coll = (Collection) mhm.get(key);

Now when you do a get() call on a multimap, it gives you a collection. The first item will be your b and the second one will be your c.

like image 31
Praba Avatar answered Sep 29 '22 02:09

Praba