Reduce repeated code by using Java8 lambdas