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.
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.
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.
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" .
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.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With