且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将对象的两个字段收集到同一个列表中?

更新时间:2022-12-12 13:53:44

您可以使用 flatMap 使用单个 Stream 管道来完成:

You can do it with a single Stream pipeline using flatMap :

List<Integer> cats = goodsList.stream()
                              .flatMap(c->Stream.of(c.getFirstCategoryID(),c.getSecondCategoryID()))
                              .collect(Collectors.toList());