Java 8 peek vs map

JavaJava 8

Java Problem Overview


I have following case: there is a list of objects - ProductData which contains several fields:

public class ProductData
{
....
  private String name;
  private String xref;
  
  //getters
  //setters
}

and there is API which returns list of following objects:

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

 //constructor
 //getters
 //setters

}

but it returns objects with empty plan name field because it's not allowed during extraction of this object. I need to link product data with RatingTableRow by the xref in order to set plan name into the RatingTableRow because I need to use this object later so I created following code to do that:

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());
        })....;

I know that java docs say that peek doesn't fit these needs but want to get your suggestions on how to make this task in more correct way.

Java Solutions


Solution 1 - Java

There is a reason peek is documented to be mainly for debugging purposes.

Something that ends up being processed inside peek might not be eligible for the terminal operation at all and streams are executed only by a terminal operation.

Suppose a trivial example first:

    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);

Everything looks fine right? Because peek will run for all elements in this case. But what happens when you add a filter (and forget about what peek did):

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

You are executing peek for every element, but collecting none. You sure you want that?

This can get even trickier:

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

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

In java-8 the list is populated, but in jdk-9 peek is not called at all. Since you are not using filter or flatmap you are not modifying the size of the Stream and count only needs it's size; thus peek is not called at all. Thus relying on peek is a very bad strategy.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - JavaEugeneView Answer on Stackoverflow