class QuizAnswers {
List<CheckboxAnswer> checkBoxAnswers;
}
class CheckboxAnswer {
int questionId;
// The indices of the selected answer choices
List<Integer> answer_selections;
}
The input to my function is a List<QuizAnswers>.
I want to create an output of Map<Integer, Map<Integer, Long>> that maps <CheckboxAnswer.questionId : <CheckboxAnswer.answer_selection, total count of answer_selection>. In other words, I want to create a nested map that maps each multiple selection quiz question to a map representing the total number of selections on each answer choice of that quiz question.
Suppose the input List<QuizAnswers> quizAnswersList as:
[ {questionId: 1, answer_selection: [1,2]},
{questionId: 1, answer_selection:[1,2,3,4]},
{questionId: 2, answer_selection:[3]},
{questionId: 2, answer_selection:[1]} ]
Then I would want the output to be:
{1 : {1:2, 2:2, 3:1, 4:1}, 2: {1:1, 3:1}}
Because the question with Id = 1 received two selections on answer choice 2 and 1 and 1 selection on answer choice 3 and 4 while the question with Id=2 had 1 selection on answer choice 1 and 3.
I have tried
quizAnswersList.stream()
.flatMap(
quizAnswers ->
quizAnswers.getCheckboxAnswers().stream())
.collect(
answer -> {
return Collectors.groupingBy(
answer.getQuestionId(),
answer.getAnswerSelections().stream()
.collect(
Collectors.groupingBy(
answerSelection -> answerSelection,
Collectors.counting())));
});
Which is giving me an error that the first collect() is not taking the right arguments.