'storing values of list

I have a Map , but I want the values of the map to be of type ArrayList

Map m = new HashMap();

since the value of the Key 'A' would itself have multiple values eg. key 'A' has values 10,20,30 please advise how to achieve this, I have created the first step below

LinkedHashMap<String,List<String>> A = new LinkedHashMap<String,List<String>>();

please advise how to add the multiple values in the list next and store it along with the Map in put operation



Solution 1:[1]

If I understand the question correctly then this seems to be the right way to me, all you then need to do is either:

List<String> strings = new ArrayList<String>();
strings.add("10");
strings.add("20");
strings.add("30");

A.put(strings);

Or you can:

A.put(Arrays.asList("10", "20", "30"));

Solution 2:[2]

Like this -

LinkedHashMap<String,List<String>> A = new LinkedHashMap<String,List<String>>();
List<String> list = new ArrayList<String>();
list.add("10");
list.add("20");
list.add("30");
A.put("a", list);

Solution 3:[3]

Like :

List<String> list = new ArrayList<>();
list.add("abc");
list.add("xyz");
// ....

Map<String,List<String>> map = new HashMap<>();
map.put("Key", list);

Solution 4:[4]

as I know that, you can use Apache MultiValueMap. It meets your requirement.http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiValueMap.html

Solution 5:[5]

Here is a program.

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


public class Test {

    public static void main(String[] args) {
        LinkedHashMap<String,List<String>> A = new LinkedHashMap<String,List<String>>();
        List<String> list = new ArrayList<>();
        list.add("1");
        A.put("1", list);

        //add new values
        list = A.get("1");
        if(list!=null){
            list.add("2");
        }else{
            list = new ArrayList<String>();
            list.add("2");
        }
        A.put("1", list);

    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tony Day
Solution 2 Subhrajyoti Majumder
Solution 3 Nandkumar Tekale
Solution 4 OQJF
Solution 5 Pankaj