Search code examples
javaarraylistnew-operatorunsafe

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/MyClass1.java 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:

classesData.get(trueDayName).add(hourData);

(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>> >();

Solution

  • 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>>());