简体   繁体   English

Java 8流-收集和流列表列表

[英]Java 8 streams - collect and stream map of lists

I have a Map of Lists, and would like to collect all the values and then stream on the collected List, so given:- 我有一个列表列表,想收集所有值,然后在收集的列表上进行流式传输,因此给出:

Map<LocalDateTime, List<PublicationSession>> effectiveDatePublicationMap;

Below works / shows what I want to do with iteration but I want to do this all in streams, rather than creating an intermediate list using iteration:- 下面的作品/显示了我想对迭代做些什么,但是我想全部在流中完成,而不是使用迭代创建中间列表:-

List< PublicationSession> publicationsToFilter = new ArrayList<>();
for (List< PublicationSession> sessions : effectiveDatePublicationMap.values()) {
    publicationsToFilter.addAll(sessions);
}

Collection< PublicationSession > wantedPublications = publicationsToFilter.stream()
            .filter(pub -> PublicationStatus.valueOf(pub.getPublishStatus()) == PublicationStatus.COMPLETE)
            .sorted(Comparator.comparing(PublicationSession::getCreateTime))
            .collect(toMap(p -> p.getPublicationSession().getEffDateTime(), UnaryOperator.identity(), PICK_LATEST))
            .values();

So I wanted to stream the map of lists all into one lists, then work on that sublist in one stream rather than having to do the above in two statements (ie build up the list by iteration, then do the streaming to filter / sort and collect. 因此,我想将列表的全部内容流式传输到一个列表中,然后在一个流中处理该子列表,而不必在两个语句中进行上述操作(即通过迭代构建列表,然后进行流式处理以进行过滤/排序和收藏。

I have so far failed to do so, when I try and stream / collect the map I get compile issues. 到目前为止,我没有这样做,当我尝试流式传输/收集地图时,出现了编译问题。 Could anyone show how to do the above in one step, without having to do iteration then streaming? 任何人都可以一步一步展示如何完成上述操作,而无需先进行迭代再进行流式处理吗?

It looks like all you need is flatMap the values of the original Map and then continue processing as you currently do: 看起来您所需要的只是flatMap原始Map的值,然后像当前一样继续处理:

Collection< PublicationSession > wantedPublications = 
    effectiveDatePublicationMap.values() // Collection<List<PublicationSession>>
                               .stream() // Stream<List<PublicationSession>>
                               .flatMap(list->list.stream()) // Stream<PublicationSession>
                               .filter(pub -> PublicationStatus.valueOf(pub.getPublishStatus()) == PublicationStatus.COMPLETE)
                               .sorted(Comparator.comparing(PublicationSession::getCreateTime))
                               .collect(toMap(p -> p.getPublicationSession().getEffDateTime(), UnaryOperator.identity(), PICK_LATEST))
                               .values();

To obtain the same result of your iterative code 为了获得相同的迭代代码结果

List< PublicationSession> publicationsToFilter = new ArrayList<>();
for (List< PublicationSession> sessions : effectiveDatePublicationMap.values()) {
    publicationsToFilter.addAll(sessions);
}

with streams you first have to use flatMap to transoform your stream of List<PublicationSession> in a stream of PublicationSession , and then collect all of them in a list with collect(Collectors.toList()) 与流你首先要使用flatMap到transoform您的流List<PublicationSession>在流PublicationSession ,然后收集所有的人都在名单collect(Collectors.toList())

List<PublicationSession> publicationsToFilter = effectiveDatePublicationMap.values()
                                                   .flatMap(Collection::stream)
                                                   .collect(Collectors.toList());

Before collecting the result, you can filter or sort as you wish. 在收集结果之前,您可以根据需要进行过滤或排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM