Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializing static Map of ArrayList

I want to define a static Map of ArrayList to include bunch of pairs of [key, ArrayList object]'s.

There is a lot to be added to these pairs during time, but they're fixed during each execution. So the best approach is initialization while defining the map of arrayList.

I can do this by a static method including:

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

and then:

map.put("key", arrayList)

However, these have to be repeated for each pair of [key, ArrayList].

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 have played with static and final values, but couldn't run it.

Finally, I could 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 of ImmutableMap in my first approach?

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

like image 863
Alisa Avatar asked Dec 29 '25 17:12

Alisa


1 Answers

Change-

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 a different ArrayList not java.util.List. The ArrayList returned is a static inner class in Arrays class.


Complete 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();
like image 173
TheLostMind Avatar answered Jan 01 '26 06:01

TheLostMind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!