【问题标题】:Writing a for loop with .map()使用 .map() 编写 for 循环
【发布时间】:2020-02-07 10:01:59
【问题描述】:

我有这段代码,它是通过 for 循环实现的。我想用 .stream 和 .map() 函数来编写它。我尝试使用 .map() 函数。但不幸的是,我收到以下错误:

不兼容的类型。必需列表> 但“收集”是 推断为 R:不存在类型变量的实例,因此 Boolean 符合 List 推理变量 T 不兼容 bounds: 等式约束: List lower bounds: Boolean

这是旧代码:

 public Iterable<Record> findAll(final List<Long> id) {
        final List<Record> result = new LinkedList<Record>();
        final List<List<Long>> partitions = ListUtils.partition(id, 10);
        for (List<Long> partition : partitions) {
            Iterables.addAll(
                    result,
                    this.repository.findAll(partition) 
            );
        }
        return result;
    }

这是我使用 .map() 时的代码

   public Iterable<Record> findAll(final List<Long> id) {
        final List<Record> result = new LinkedList<Record>();
        final List<List<Long>> partitions = ListUtils.partition(id, 10);

         List<List<Long>> allPartitions = partitions.stream().map(partition ->{
            return Iterables.addAll(result, this.repository.findAll(partition));
        }).collect(Collectors.toList());

        return result;
    }

关于如何解决此问题的任何建议?或者我应该注意什么?

【问题讨论】:

  • 试试这个List&lt;List&lt;Long&gt;&gt; allPartitions = partitions.stream().map(partition -&gt;{ Iterables.addAll(result, this.repository.findAll(partition)); return result; }).collect(Collectors.toList());
  • @HadiJ 我看不出我写的代码有什么不同,而且没有工作
  • Iterables.addAll(result, this.repository.findAll(partition)); 返回布尔值,而您需要返回 List&lt;Long&gt; 因为它不兼容。所以我想你应该返回result 虽然我不熟悉番石榴。
  • 或者这样return partitions.stream() .flatMap(partition-&gt;this.repository.findAll(partition).stream()) .collect(Collectors.toList());
  • this.repository.findAll(partition) 返回什么?

标签: java spring-boot java-8 java-stream


【解决方案1】:

假设this.repository.findAll(partition)返回Iterable&lt;Record&gt;,你可以使用

public Iterable<Record> findAll(final List<Long> id) {
    return ListUtils.partition(id, 10).stream()
        .flatMap(partition -> StreamSupport.stream(
                this.repository.findAll(partition).spliterator(), false))
        .collect(Collectors.toList());
}

这种复杂的Stream 结构只有在findAll 返回Iterable 而不是Collection 时才需要。如果它返回Collection,您可以简单地使用:

public Iterable<Record> findAll(final List<Long> id) {
    return ListUtils.partition(id, 10).stream()
        .flatMap(partition -> this.repository.findAll(partition).stream())
        .collect(Collectors.toList());
}

在这方面,您不应将同样的负担放在方法的调用者身上,而应考虑将返回类型从 Iterable&lt;Record&gt; 更改为 Collection&lt;Record&gt; 甚至 List&lt;Record&gt;

要考虑的另一点是,当您检索所有记录时,在这些小块中执行此操作是否真的有任何好处。
那么为什么不直接使用return this.repository.findAll(id);...

【讨论】:

  • 它看起来更像一个游标用例。 (但不太确定弹簧靴的内部结构。)
【解决方案2】:

我猜你可以用forEach替换for循环:

public Iterable<Record> findAll(final List<Long> id) {
    final List<Record> result = new LinkedList<Record>();
    final List<List<Long>> partitions = ListUtils.partition(id, 10);
    partitions.forEach(partition -> Iterables.addAll(result, this.repository.findAll(partition)));
    return result;
}

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 2014-11-28
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多