Java 8 peek vs map

Viewed 45293

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.

1 Answers
Related