Skip to content

Instantly share code, notes, and snippets.

@john77eipe
Created April 16, 2017 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john77eipe/342ca8eee12e961edca9d25a406c6089 to your computer and use it in GitHub Desktop.
Save john77eipe/342ca8eee12e961edca9d25a406c6089 to your computer and use it in GitHub Desktop.
//Vendor code
static class IntegerAddition implements BinaryOperator<Integer> {
static private IntegerAddition instance = new IntegerAddition() ;
private IntegerAddition() {}
@Override
public Integer apply(Integer t, Integer u) {
return t + u;
}
public static IntegerAddition instance() {
return instance;
}
}
public static <T extends Number> Optional<T> sum(List<T> list, Predicate<T> condition, BinaryOperator<T> operation){
return list.parallelStream()
.filter(condition)
.map(i -> i)
.reduce(operation);
}
//client
int result = MathUtility.sum(listOfInts, i->i<4, MathUtility.IntegerAddition.instance()).get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment