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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.