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?
source to share
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();
source to share
-
The ClassCastException comes from line 11 where you are casting 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 ArrayList. So to fix this, you can either change the values stored in your map to a list or manually create your ArrayLists rather than use Arrays.asList ().
-
It's ok if you are not serializing anything. See What it means: Does the serializable class declare a static final field serialVersionUID? ...
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?
source to share