3

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?

Community
  • 1
  • 1
Alisa
  • 2,892
  • 3
  • 31
  • 44

2 Answers2

4

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();
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
2
  1. The ClassCastException is coming from line 11, where you cast the returned object from Arrays.asList() to ArrayList. The problem is that Arrays.asList() returns objects of type List (see http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html), and List is not a subtype of ArrayList. So to fix this, you can either change the values stored in your Map to List or manually build your ArrayLists instead of using Arrays.asList().

  2. That's ok if you're not serializing anything. See What does it mean: The serializable class does not declare a static final serialVersionUID field?.

I suspect there's another problem with the way you are using ImmutableMap, but I don't understand your question so I can't comment on that. Could you rephrase the intro of your question?

Community
  • 1
  • 1
11th Hour Worker
  • 337
  • 3
  • 14
  • Thank you for your both answers. For the first question, your comment works as well as the answer of @ThelostMind. – Alisa Aug 20 '14 at 18:27