使用流和收集器创建嵌套地图的问题
class QuizAnswers {
List<CheckboxAnswer> checkBoxAnswers;
}
class CheckboxAnswer {
int questionId;
// The indices of the selected answer choices
List<Integer> answer_selections;
}
我功能的输入是 list&lt; quizanswers&gt;
。
我想创建一个 map&lt; integer,map&lt; integer,long&gt;&gt;
映射&lt; chackboxanswer.questionid:&lt; chackboxanswer.answer.answer.answer.answer_selection代码>。换句话说,我想创建一个嵌套地图,将每个多个选择测验问题映射到代表该测验问题的每个答案选择总数的地图。
假设输入 list&lt; quizanswers&gt; 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]} ]
然后我希望输出为:
{1 : {1:2, 2:2, 3:1, 4:1}, 2: {1:1, 3:1}}
因为 id = 1
接收到答案选择的两个选择 2
和 1 < /code>和
1
在答案选择上选择 3
和 4
iD = 2
hate > 1
在答案选择上选择 1
和 3
。
我已经尝试过
quizAnswersList.stream()
.flatMap(
quizAnswers ->
quizAnswers.getCheckboxAnswers().stream())
.collect(
answer -> {
return Collectors.groupingBy(
answer.getQuestionId(),
answer.getAnswerSelections().stream()
.collect(
Collectors.groupingBy(
answerSelection -> answerSelection,
Collectors.counting())));
});
,这给我一个错误,即第一个收集()没有采用正确的论点。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管您很接近,但您对收藏家的使用在句法上是不正确的。
首先,必需的方法
collect> collect()
期望收集器(也有另一种味道collect> collect> collect()
期望树“函数”:供应商,累加器和组合者; .oracle.com/en/java/javase/17/docs/api/java.base/java/java/util/stream/collectors.html#groupingby(java.util.function.function.function.function.function.function.function)“ rel =” nofollow noreforler noreferrer“ noreferrer”> <代码> groupingby() 收集器。我们需要它的版本,它可以期望
classifier
函数和a downstream collector :作为下游收集器,我们需要提供
flatmapping()
。期望将每个checkboxanswer
转换为答案选择值的流() >)和A 下游收集器。依次为flatmapping()
的下游再次提供groupingby()
and andcounting()
as它的下游收集器。收藏家的最终结构将是以下内容:
现在,让我们将所有内容放在一起。
输出
Although you were close, your usage of collectors is syntactically incorrect.
Firstly, the required method
collect()
expects a collector (there's also another flavor ofcollect()
that expect tree "functions": supplier, accumulator and combiner; don't confuse them), that's the correct syntax:Now, regarding the
groupingBy()
collector. We need its version that expects aclassifier
function and a downstream collector:And as a downstream collector we need to provide
flatMapping()
. Which expects a function that should transform eachCheckboxAnswer
into a stream of answer selection values (in order to be able to map each of them to its count), and a downstream collector. In turn as the downstream offlatMapping()
again we need to providegroupingBy()
andcounting()
as its downstream collector.The final structure of collectors will be the following:
Now, let's put all the things together.
Output
分两个步骤可以更容易地到达最终结果:按照
质疑
进行分组后,您需要将映射到answers_selections
。可以使用collector.mapping
来完成此操作类似:这将为您提供类似的输出:
由于以上不是您真正想要的,因此您需要再做一个步骤并包装在此处完成的映射
。上述映射的值,即列表的类型列表的类型列表列出到
map&lt; integer,long&gt;
,可以如下所示(包括上述步骤,这仅对解释中间结果有用,仅是有用的需要以下代码):这将为您带来所需的结果
It is may be easier to get to the final result in two steps: After grouping by your
questionId
you need to map to youranswer_selections
. This can be done usingCollectors.mapping
so that you end up with an intermediate result ofMap<Integer,List<List<Integer>>>
which could look like something like:This will give you an output like:
Since the above is not what you realy wanted you need to do one more step and wrap the mapping which is done here
in to
Collectors.collectingAndThen
to turn the values of above map which are of type list of lists to aMap<Integer,Long>
which can be done like showed below (including the above step, which only was useful to explain the intermediate result, only the code below is needed):which will give you the desired result