使用流和收集器创建嵌套地图
class QuizAnswers {
List<MultipleChoiceAnswer> multipleChoiceAnswers;
List<FreeResponseAnswer> freeResponseAnswers; //not relevant to this question
}
class MultipleChoiceAnswer {
int questionId;
// The index of the selected multiple choice question
int answer_selection;
}
我功能的输入是list&lt; quizanswers&gt;
。
我想创建一个map&lt; integer的输出,映射,long&gt;&gt;
,映射映射&lt; multiplechoiceanswer.questionId:&lumertipleChoiceAceAnswer.answer_answer_swer_selection代码>。换句话说,我想创建一个嵌套的地图,将每个多项选择测验问题映射到表示该测验问题每个答案选择总数的地图。
假设输入list&lt; quizanswers&gt; quizanswerslist
as:
[ {questionId: 1, answer_selection: 2},
{questionId: 1, answer_selection:2},
{questionId: 1, answer_selection:3},
{questionId: 2, answer_selection:1} ]
然后我希望输出为:
{1 : {2:2, 3:1}, 2: {1, 1}}
因为id = 1
接收到答案选择的两个选择2
和1 < /code>在答案选择上选择
3
id = 2
hate 1
选择答案选择1
。
我已经尝试过,
quizAnswersList.stream()
.map(
quizAnswers ->
quizAnswers.getMultipleChoiceAnswers().stream()
.collect(
Collectors.groupingBy(
MultipleChoiceAnswer::getQuestionId,
Collectors.groupingBy(
MultipleChoiceAnswer::getAnswerSelection,
Collectors.counting()))));
这给了我一个错误。我对流的溪流和收藏家并不熟悉,所以我很想学习如何正确执行此操作。
class QuizAnswers {
List<MultipleChoiceAnswer> multipleChoiceAnswers;
List<FreeResponseAnswer> freeResponseAnswers; //not relevant to this question
}
class MultipleChoiceAnswer {
int questionId;
// The index of the selected multiple choice question
int answer_selection;
}
The input to my function is a List<QuizAnswers>
.
I want to create an output of Map<Integer, Map<Integer, Long>>
that maps <MultipleChoiceAnswer.questionId : <MultipleChoiceAnswer.answer_selection, total count of answer_selection>
. In other words, I want to create a nested map that maps each multiple choice 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: 2},
{questionId: 1, answer_selection:2},
{questionId: 1, answer_selection:3},
{questionId: 2, answer_selection:1} ]
Then I would want the output to be:
{1 : {2:2, 3:1}, 2: {1, 1}}
Because the question with Id = 1
received two selections on answer choice 2
and 1
selection on answer choice 3
while the question with Id=2
had 1
selection on answer choice 1
.
I have tried
quizAnswersList.stream()
.map(
quizAnswers ->
quizAnswers.getMultipleChoiceAnswers().stream()
.collect(
Collectors.groupingBy(
MultipleChoiceAnswer::getQuestionId,
Collectors.groupingBy(
MultipleChoiceAnswer::getAnswerSelection,
Collectors.counting()))));
Which is giving me an error. I am not very familiar with streams and collectors in general, so I'd love to learn how to do this correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你很近。您只是没有
flatmap
多人杂志
在流上,因此您有一个嵌套的流,这引起了问题。根据您编辑的问题,这是我想到的。
MultipleChoice
listsquestionId
AnswerSelection
and get a count打印
问题
quizanswer
实例?mulitplechoiceanswer
列表。您只提供了其中一个。它们都可以是
flatmapped
一起处理,并如上所述进行处理。但是我认为答案(也许对于不同的测试)可能会有一些差异,而您不希望将其分组和算作相同。示例
如果我将以下内容添加到
list&lt; quizanswers&gt;
和使用上述解决方案的过程中,则输出将为
You were close. You just didn't
flatMap
theMultipleChoiceAnswers
onto the stream so you had a nested stream and that was causing the problem.Based on your edited question, here is what I came up with.
MultipleChoice
listsquestionId
AnswerSelection
and get a countprints
Questions
QuizAnswer
instances?MulitpleChoiceAnswer
lists. You only provided one of each.They could all be
flatmapped
together and processed as above. But I think there could be some differences in the Answers (perhaps for different tests) which you don't want grouped and counted as the same.Example
If I add the following to the
List<QuizAnswers>
And process using the above solution, the output would be
方法
map()
是一个中间操作,即它产生a stream 。因此,如果您尝试将 stratement 分配给您已列出的类型map
的变量,则会遇到汇编错误。流管线需要以
collect
之类的终端操作结束,以便被执行并产生结果。collect()
您必须应用flatmap()
,期望流>作为一个参数,将quizanswers
的流将其转换为多人杂货>的流
。您对收集器的使用是正确的,不需要任何更改。
输出
Method
map()
is an intermediate operation, i.e. it yields a stream. Therefore, if you try to assign the stream-statement you've listed to a variable of typeMap
you'll get a compilation error.A stream pipeline needs to end with a terminal operation like
collect
in order to be executed and produce a result.And before
collect()
you have to applyflatMap()
, which expects a stream as an argument, to transform the stream ofQuizAnswers
in to a stream ofMultipleChoiceAnswer
.Your usage of collectors is correct and doesn't require any changes.
Output