Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stream / Reduce a series of functions applied to an input value with bound variables in Java

How does one go about rewriting this loop using a stream.

private final List<RegionSpecificValidator> validators;
public Item applyValidations(Item inbound, Holder holder, Product product) {
    Item toValidate = inbound;
    for (var validator: validators)
        toValidate = validator.validate(toValidate, holder, product);
    return toValidate;
}

It should be possible to rewrite this loop using validators.stream().reduce(...)

I have tried using the guidance here, but I cannot figure out what BiFunction interface needs to be implemented and how.
The interface as it currently stands looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public interface RegionSpecificValidator {
    Item validate(Item toValidate, Holder holder, Product product);
}

I have looked at this guidance: Reducing a list of UnaryOperators in Java 8
However it’s not clear how I implement andThen in this case, or even if it’s possible.

>Solution :

You need to use the reduce() method with transformer and combiner.

The three arguments are as follows:

  • inbound is the base case. For example, if validators is an empty list, it is the object that is returned by default
  • (toValidate, validator) -> validator.validate(toValidate, holder, product) it takes an Item and a validator and produces an Item. This happens via the validate() method on the validator.
  • (prev, next) -> next defines how reduction should happen between two items – you need to pick one. In your for-loop you are always overwriting toValidate with the new value, so in this case we should return next, i.e. the last element that we obtained from validator.validate().
public Item applyValidations(Item inbound, Holder holder, Product product) {
    return validators.stream().reduce(
        inbound, 
        (toValidate, validator) -> validator.validate(toValidate, holder, product), 
        (prev, next) -> next
    );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading