-1

Is there a way to access each value to assign it to a variable from a java MultiValueMap from Apache commons while iterating? I have one key and two possible values that I would like to extract each iteration that are later written to a table.

Currently the below produces two values for pair.getValue().

public void setValues (MultiValueMap map)
{
    Iterator it = map.entrySet().iterator();
    while (it.hasNext())
    {
        Map.Entry pair = (Map.Entry)it.next();
        String id = pair.getKey().toString();
        String name = pair.getValue().toString();
    }
}

I edited this for clarity, a below suggestion was helpful in using Collection values = map.getCollection(id);

caro
  • 863
  • 3
  • 15
  • 36

1 Answers1

1

If I understand your question, you want to get the list of values for all keys:

    Iterator it = map.keySet().iterator();
    while (it.hasNext())
    {
        String id = (String)it.next();
        Collection<?> values = map.getCollection(id);
        // loop on values and do whatever you need ...
    }
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • no i need to access each of the values associated with a key, one is a code, another is a longer string also related to the code. I would ideally like the next line to be String x = pair.getValue.toString() so I can have id, name, and x to insert into a table in an xml mapper. – caro Jun 16 '15 at 12:46
  • You have access to each value with the code I gave you. MultiValueMap assosicates a Collection of Values with one Key. `pair.getValue` gives you a collection and doing `toString()` on this collection concatanates it into one String. – Sharon Ben Asher Jun 16 '15 at 12:51
  • we are talkiung about MultiValueMap from Apache commons collections, right? – Sharon Ben Asher Jun 16 '15 at 12:53
  • ah yes, i must have misunderstood – caro Jun 16 '15 at 12:55