1

I have made two Lists like

List<LearnerEnrollment> learnerEnrollmentList = new ArrayList<LearnerEnrollment>();
List<LearnerCourseEnrollError> enrollErrorList = new ArrayList<LearnerCourseEnrollError>();

Then i made two Maps like

Map<String, List<LearnerCourseEnrollError>> courseErrorMap = new HashMap<String, List<LearnerCourseEnrollError>>();
Map<String, List<LearnerEnrollment>> courseSuccessMap = new HashMap<String, List<LearnerEnrollment>>();

Then i made another Map to hold the above two Maps like

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

Then i use the following code to add items in lists;

for (com.softech.vu360.lms.model.Course course : courseList) {

    Object result = getEnrollmentForCourse(customer, learner, course);

    if (result instanceof LearnerEnrollment) {
        LearnerEnrollment newEnrollment = (LearnerEnrollment)result;
        learnerEnrollmentList.add(newEnrollment);
    } else if (result instanceof String) { 
        String errorMessage = (String)result;
        LearnerCourseEnrollError enrollError = new LearnerCourseEnrollError(errorMessage, course);
        enrollErrorList.add(enrollError);       
    }       
}

Now i am putting values in the Map

courseSuccessMap.put(learner.getVu360User().getUsername(), learnerEnrollmentList);
courseErrorMap.put(learner.getVu360User().getUsername(), enrollErrorList);
courseMap.put("successfulCoursesMap", courseSuccessMap);
courseMap.put("unSuccessfulCoursesMap", courseErrorMap);

return courseMap;

But i am getting error at these two lines

courseMap.put("successfulCoursesMap", courseSuccessMap);
courseMap.put("unSuccessfulCoursesMap", courseErrorMap);

that

The method put(String, Map<String,List<Object>>) in the type
Map<String,Map<String,List<Object>>> is not applicable for the arguments 
(String, Map<String,List<LearnerEnrollment>>)

The method put(String, Map<String,List<Object>>) in the type
Map<String,Map<String,List<Object>>> is not applicable for the arguments 
(String, Map<String,List<LearnerCourseEnrollError>>)

Why?

My list type in the Map is List<Object> and List<LearnerEnrollment> is List <Object> because LearnerEnrollment extends Object. Why I am getting these errors ?

If i declare my Map like this

Map<String, Map<String, ?>> courseMap = new HashMap<String, Map<String, ?>>();

Then there is no error. Why i am getting error in first case?

Thanks

Basit
  • 8,426
  • 46
  • 116
  • 196

2 Answers2

6

You said:

List<LearnerEnrollment> is List<Object>

This is wrong. If it were true, you would be able to do:

List<<LearnerEnrollment> list = new ArrayList<>();
List<Object> objectList = list;
objectList.add("Now what?");

And your type-safe list of LearnerEnrollment would suddenly contain a String.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

This is because a List in java is not covariant. A List<LearnerEnrollment> is not a subclass of List<Object>.

See Java covariance for more information.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171