如何使用流获取嵌套集合中的所有元素

我有一个包含不同嵌套集合的类,现在我想接收嵌套集合的所有元素,具体我想收集集合的所有 StrokePoints。我可以用“旧的”java 来解决它,但是如何用流来解决呢?


    int strokesCounter = 0;

    List<StrokePoint> pointList = new ArrayList<>();

    if (!strokesData.getListOfSessions().isEmpty()) {

        for (SessionStrokes session : strokesData.getListOfSessions()) {

            List<Strokes> strokes = session.getListOfStrokes();

            for (Strokes stroke : strokes) {

                strokesCounter++;

                List<StrokePoint> points = stroke.getListOfStrokePoints();

                pointList.addAll(stroke.getListOfStrokePoints());        

            }

        }

    }

我正在寻找一种使用流功能填充 pointList 的方法。


慕尼黑8549860
浏览 134回答 4
4回答

波斯汪

展平嵌套数据非常简单:List<StrokePoint>&nbsp;pointList&nbsp;=&nbsp;strokesData.getListOfSessions() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.streams() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(SessionStrokes::getListOfStrokes) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(List::stream) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(Strokes::getListOfStrokePoints) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(List::stream) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());沿途收集划水次数比较棘手,而且有些争议。你可以创建一个AtomicInteger&nbsp;strokesCounter&nbsp;=&nbsp;new&nbsp;AtomicInteger();并在第一个之后增加它flatMap:.peek(strokesCounter::incrementAndGet)

白板的微信

你可以只使用Stream.flatMap()两次:List<StrokePoint>&nbsp;pointList&nbsp;=&nbsp;strokesData.getListOfSessions().stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(session&nbsp;->&nbsp;session.getListOfStrokes().stream()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(strokes&nbsp;->&nbsp;strokes.getListOfStrokePoints().stream()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());如果您需要计算笔画列表,您可以将其分成两部分并使用List.size():List<Strokes>&nbsp;strokesList&nbsp;=&nbsp;strokesData.getListOfSessions().stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(session&nbsp;->&nbsp;session.getListOfStrokes().stream()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());int&nbsp;strokesCounter&nbsp;=&nbsp;strokesList.size(); List<StrokePoint>&nbsp;pointList&nbsp;=&nbsp;strokesList.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(strokes&nbsp;->&nbsp;strokes.getListOfStrokePoints().stream()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());或者你可以增加一个AtomicIntegerin&nbsp;flatMap():final&nbsp;AtomicInteger&nbsp;strokesCounter&nbsp;=&nbsp;new&nbsp;AtomicInteger(); List<StrokePoint>&nbsp;pointList&nbsp;=&nbsp;strokesData.getListOfSessions().stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(session&nbsp;->&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List<Strokes>&nbsp;strokes&nbsp;=&nbsp;session.getListOfStrokes(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strokesCounter.addAndGet(strokes.size());&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;strokes.stream(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(strokes&nbsp;->&nbsp;strokes.getListOfStrokePoints().stream()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());或与peek():final&nbsp;AtomicInteger&nbsp;strokesCounter&nbsp;=&nbsp;new&nbsp;AtomicInteger(); List<StrokePoint>&nbsp;pointList&nbsp;=&nbsp;strokesData.getListOfSessions().stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(session&nbsp;->&nbsp;session.getListOfStrokes().stream()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.peek(i&nbsp;->&nbsp;strokesCounter.incrementAndGet()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(strokes&nbsp;->&nbsp;strokes.getListOfStrokePoints().stream()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());

慕后森

由于主要目的是解决收集 的问题List<StrokePoint>,您可以使用以下flatMap操作执行它:List<StrokePoint> points = strokesData.getListOfSessions()&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(ss -> ss.getListOfStrokes().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(s -> s.getListOfStrokePoints().stream()))&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());此外,Strokes 的计数也可以通过对列表的大小求和来使用流来计算:long strokeCount = strokesData.getListOfSessions()&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .mapToLong(ss -> ss.getListOfStrokes().size())&nbsp; &nbsp; &nbsp; &nbsp; .sum();要合并这些操作,您可以构造AbstractMap.SimpleEntrywhile 减少条目为:AbstractMap.SimpleEntry<Integer, Stream<StrokePoint>> reduce = strokesData.getListOfSessions()&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .map(ss -> new AbstractMap.SimpleEntry<>(ss.getListOfStrokes().size(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ss.getListOfStrokes()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(s -> s.getListOfStrokePoints().stream())))&nbsp; &nbsp; &nbsp; &nbsp; .reduce(new AbstractMap.SimpleEntry<>(1, Stream.empty()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (e1, e2) -> new AbstractMap.SimpleEntry<>(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer.sum(e1.getKey(), e2.getKey()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stream.concat(e1.getValue(), e2.getValue())));使用此条目,您可以获得 s 的计数和Strokes 的列表StrokePoint:long strokeCount = reduce.getKey();List<StrokePoint> strokePoints = reduce.getValue().collect(Collectors.toList());

江户川乱折腾

如果您担心副作用,这样做就可以了(但是在撰写本文时其他两个答案都更具可读性):&nbsp; &nbsp; Entry<Integer, List<StrokePoint>> summary = //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strokesData.getListOfSessions()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.flatMap(session -> session.getListOfStrokes().stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(strokes -> new SimpleEntry<>(1, strokes.getListOfStrokePoints()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.reduce((l1, l2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int count = l1.getKey() + l2.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List<StrokePoint> list = l1.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.addAll(l2.getValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new SimpleEntry<>(count, list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.orElse(new SimpleEntry<>(0, Collections.emptyList()));&nbsp; &nbsp; strokesCounter = summary.getKey();&nbsp; &nbsp; pointList = summary.getValue();编辑以添加验证:public class Scratch {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int strokesCounter = 0;&nbsp; &nbsp; &nbsp; &nbsp; List<StrokePoint> pointList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; StrokesData strokesData = new StrokesData();&nbsp; &nbsp; &nbsp; &nbsp; SessionStrokes sessionStrokes = new SessionStrokes();&nbsp; &nbsp; &nbsp; &nbsp; strokesData.sessionStrokes.add(sessionStrokes);&nbsp; &nbsp; &nbsp; &nbsp; Strokes s1 = new Strokes();&nbsp; &nbsp; &nbsp; &nbsp; sessionStrokes.strokesList.add(s1);&nbsp; &nbsp; &nbsp; &nbsp; s1.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; s1.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; Strokes s2 = new Strokes();&nbsp; &nbsp; &nbsp; &nbsp; sessionStrokes.strokesList.add(s2);&nbsp; &nbsp; &nbsp; &nbsp; s2.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; s2.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; s2.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; s2.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; s2.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; s2.strokePoints.add(new StrokePoint());&nbsp; &nbsp; &nbsp; &nbsp; Entry<Integer, List<StrokePoint>> summary = //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strokesData.getListOfSessions()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.flatMap(session -> session.getListOfStrokes()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(strokes -> new SimpleEntry<>(1, strokes.getListOfStrokePoints()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.reduce((l1, l2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int count = l1.getKey() + l2.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List<StrokePoint> list = l1.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.addAll(l2.getValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new SimpleEntry<>(count, list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.orElse(new SimpleEntry<>(0, Collections.emptyList()));&nbsp; &nbsp; &nbsp; &nbsp; strokesCounter = summary.getKey();&nbsp; &nbsp; &nbsp; &nbsp; pointList = summary.getValue();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(strokesCounter);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(pointList);&nbsp; &nbsp; }}class StrokesData {&nbsp; &nbsp; List<SessionStrokes> sessionStrokes = new ArrayList<>();&nbsp; &nbsp; public List<SessionStrokes> getListOfSessions() {&nbsp; &nbsp; &nbsp; &nbsp; return sessionStrokes;&nbsp; &nbsp; }}class SessionStrokes {&nbsp; &nbsp; List<Strokes> strokesList = new ArrayList<>();&nbsp; &nbsp; public List<Strokes> getListOfStrokes() {&nbsp; &nbsp; &nbsp; &nbsp; return strokesList;&nbsp; &nbsp; }}class Strokes {&nbsp; &nbsp; List<StrokePoint> strokePoints = new ArrayList<>();&nbsp; &nbsp; public List<StrokePoint> getListOfStrokePoints() {&nbsp; &nbsp; &nbsp; &nbsp; return strokePoints;&nbsp; &nbsp; }}class StrokePoint {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java