Initializing static map ArrayList

I want to define an old map from an ArrayList to include a bunch of [key, ArrayList object] pairs .

Many of these pairs need to be added at runtime, but they are captured at runtime. Therefore, the best approach is to initialize when defining the arrayList map .

I can do this with a static method including:

arrayList.add("value1"), 
arrayList.add("value2"), 
arrayList.add("value3"),
... 

      

and then:

map.put("key", arrayList)

      

However, these must be repeated for each [key, ArrayList] pair.

I tried ImmutableMap but got this error:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
    at bugTriaging.Constants.<clinit>(Constants.java:11)

      

Here's my code:

package package1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.ImmutableMap;

public class Constants { 
    static final Map<String, ArrayList<String>> map1 = ImmutableMap.<String, ArrayList<String>>builder()
            .put("key1", (ArrayList<String>) Arrays.asList("value1", "value2", "value3"))
            .put("key2", (ArrayList<String>) Arrays.asList("value4", "value5", "value6", "value7"))
            .put("key3", (ArrayList<String>) Arrays.asList("value8", "value9", "value10", "value11", "value12", "value13"))
            .build();

    public static void main(String[] args) {
        System.out.println("Started ...");
    }
}

      

I played around with static and final values ​​but couldn't get it to run.

Finally, I can run this code:

    @SuppressWarnings("serial") //to avoid the warning.
    public static HashMap<String, ArrayList<String>> map2 = new HashMap<String, ArrayList<String>>() {{
        put("key1", new ArrayList<String>() {{
            add("value1");
            add("value2");
          }});
        put("key2", new ArrayList<String>() {{
            add("value3");
            add("value4");
            add("value5");
          }});
        put("key3", new ArrayList<String>() {{
            add("value6");
            add("value7");
            add("value8");
          }});
    }};

      

Here are my questions:

1- What is the problem with ImmutableMap in my first approach?

2- With my solution, I get warnings ("The serializable class does not declare a static final field serialVersionUID of type long "). Does @SuppressWarnings ("serial") add the correct way to get rid of the warnings or is there a better way?

+3


source to share


2 answers


Edit

static final Map<String, ArrayList<String>> map1 = ImmutableMap.<String, ArrayList<String>>builder() 

      

to

static final Map<String, List<String>> map1 = ImmutableMap.<String, List<String>>builder()

      

Why??



Arrays.asList()

returns another ArrayList

not java.util.List

. The returned ArrayList is a static inner class within the class Arrays

.


Full initialization:

static final Map<String, List<String>> map1 = ImmutableMap
        .<String, List<String>> builder()
        .put("key1", Arrays.asList(new String[] { "value1", "value2", "value3" }))
        .put("key2", Arrays.asList(new String[] { "value4", "value5", "value6", "value7" }))
        .put("key3",Arrays.asList(new String[]  { "value8", "value9", "value10", "value11", "value12", "value13" })).build();

      

+4


source




I suspect there is another problem with the way you use ImmutableMap, but I don't understand your question, so I cannot comment on this. Could you rephrase the introduction to your question?

+2


source







All Articles