Gatonito Gatonito - 1 year ago 74
Java Question

new ArrayList() as an argument to a map.put() is an unsafe operation?

The following code:

for (String day:daysOfWeek) {classesData.put(day, new ArrayList());}

gives me the following error:

Note: ./com/myname/MyClass/ uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I had to manually comment a lot of lines to see that the code above was the one with error. I suspect it's because of
classesData.put(day, new ArrayList());
, more specifically,
new ArrayList()
. I'm starting
new ArrayList()
because I want to add things in the future, now now. I add like this:


(hourData is a Map)

because classesData is the following data structure:

Map<String, List<Map<String, String>>> classesData = new HashMap<String, List<Map<String, String>> >();

Answer Source

ArrayList in Java is a generic type and you're omitting the generics completely and using it as a raw type. I'm not certain, but this could be the reason for your issue.

Try: classesData.put(day, new ArrayList<Map<String, String>>());