Java 8偷看vs地图

时间:2017-06-05 13:58:13

标签: java java-8

我有以下案例:有一个对象列表 - ProductData包含几个字段:

public class ProductData
{
....
  private String name;
  private String xref;

  //getters
  //setters
}

并且有API返回以下对象的列表:

public class RatingTableRow
{
  private String planName;
  private String planXref;
  private int fromAge;
  private int toAge;
  private int ratingRegion;

 //constructor
 //getters
 //setters

}

但它返回具有空计划名称字段的对象,因为在提取此对象期间不允许这样做。我需要通过外部参照将产品数据与RatingTableRow链接,以便将计划名称设置为RatingTableRow,因为我需要稍后使用此对象,因此我创建了以下代码来执行此操作:

Map<String, ProductData> productByXref = plans.stream()
        .collect(toMap(ProductData::getInternalCode, Function.identity()));

return getRatingTableRows(...).stream
        .filter(ratingRow -> productByXref.containsKey(ratingRow.getPlanXref()))
        .peek(row -> {
                ProductData product = productByXref.get(row.getPlanXref());
                row.setPlanName(product.getName());
        })....;

我知道java文档说peek并不适合这些需求,但希望得到关于如何以更正确的方式完成此任务的建议。

1 个答案:

答案 0 :(得分:38)

有一个原因peek被记录为主要用于调试目的

最终在peek内处理的内容可能根本不符合终端操作的条件,并且流只能通过终端操作执行。

首先假设一个简单的例子:

    List<Integer> list = new ArrayList<>();
    List<Integer> result = Stream.of(1, 2, 3, 4)
            .peek(x -> list.add(x))
            .map(x -> x * 2)
            .collect(Collectors.toList());

    System.out.println(list);
    System.out.println(result);

一切看起来都不错吧?因为peek将在这种情况下为所有元素运行。但是当您添加filter(并忘记peek做了什么)时会发生什么:

 .peek(x -> list.add(x))
 .map(x -> x * 2)
 .filter(x -> x > 8) // you have inserted a filter here

您正在为每个元素执行peek,但不收集任何。你确定要的吗?

这可能变得更加棘手:

    long howMany = Stream.of(1, 2, 3, 4)
            .peek(x -> list.add(x))
            .count();

    System.out.println(list);
    System.out.println(howMany);

在java-8中填充了列表,但是在jdk-9 peek中根本没有被调用。由于您未使用filterflatmap,因此您不会修改流的大小,count只需要它的大小; 因此根本不会调用。因此,依赖peek是一个非常糟糕的策略。