【问题标题】:Java 8 stream api how to collect List to ObjectJava 8 流 api 如何收集 List 到 Object
【发布时间】:2016-08-19 17:44:06
【问题描述】:

我有两个简单的类 ImageEntity 和 ImageList

如何将结果列表 ImageEntity 收集到 ImageList 中?

List<File> files = listFiles();
        ImageList imageList = files.stream().map(file -> {
            return new ImageEntity(
                                   file.getName(), 
                                   file.lastModified(), 
                                   rootWebPath + "/" + file.getName());
        }).collect(toCollection(???));

public class ImageEntity {
private String name;
private Long lastModified;
private String url;
 ...
}

public class ImageList {
 private List<ImageEntity> list;

 public ImageList() {
    list = new ArrayList<>();
 }

 public ImageList(List<ImageEntity> list) {
    this.list = list;
 }
 public boolean add(ImageEntity entity) {
    return list.add(entity);
 }
 public void addAll(List<ImageEntity> list) {
     list.addAll(entity);
 }

}

这不是一个优雅的解决方案

ImageList imgList = files.stream().
  .map(file -> { return new ImageEntity(file.getName(), file.lastModified(), rootWebPath + "/" + file.getName()) })
  .collect(ImageList::new, (c, e) -> c.add(e), (c1, c2) -> c1.addAll(c2));

通过collectAndThen可以解决吗?

还有什么想法?

【问题讨论】:

    标签: lambda java-8 java-stream collect collectors


    【解决方案1】:

    collectingAndThen 方法的缺点是创建一个列表然后复制它。

    如果您想要比最初的 collect 示例更可重用的东西,并且像您的示例一样,不会结束在 collectingAndThen 收集器中进行额外复制,您可以采用 @ 的三个参数987654323@ 并创建一个类似于Collectors.toList() 的函数,该函数直接收集到您的 ImageList,如下所示:

    public static Collector<ImageEntity,?,ImageList> toImageList() {
        return Collector.of(ImageList::new, (c, e) -> c.add(e), (c1, c2) -> c1.addAll(c2));
    }
    

    然后你可以像这样使用它:

    ImageList imgList = files.stream().
        .map(file -> new ImageEntity(file.getName(), file.lastModified(), rootWebPath + "/" + file.getName()))
        .collect(toImageList());
    

    【讨论】:

      【解决方案2】:

      你也可以试试下面的

      ImageList imgList = new ImageList (files.stream().
        .map(file -> { return new ImageEntity(file.getName(), file.lastModified(), rootWebPath + "/" + file.getName()) })
        .collect(Collectors.toList()));
      

      【讨论】:

      • 这真的很奇怪, Collectors.toList() 如何返回 Object 而不是对象列表。你能解释一下吗?
      • 这是我的错误,我认为 ImageListList&lt;Images&gt; 。我们应该有类似.collect(converttoImageList());
      【解决方案3】:

      由于ImageList 可以从List&lt;ImageEntity&gt; 构造,您可以使用Collectors.collectingAndThen

      import static java.util.stream.Collectors.toList;
      import static java.util.stream.Collectors.collectingAndThen;
      
      ImageList imgList = files.stream()
          .map(...)
          .collect(collectingAndThen(toList(), ImageList::new));
      

      另外,您不必在 lambda 表达式中使用花括号。你可以使用file -&gt; new ImageEntity(file.getName(), file.lastModified(), rootWebPath + "/" + file.getName())

      【讨论】:

      • 好!非常优雅!非常感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多