In this Blog we will be reading about creating a high order function from a bifunction and predicate in java8.Firstly a brief introduction to bifunction,predicate,high order function in java8
Predicate:Predicate in java 8 is used to apply a filter to a collection of objects.Predicate is a Functional interface and supports Lambda expressions. Separating Predicates that way makes them reusable and you can achieve separation of business layer and domain layer
BiFunction: BiFunction is a functional interface which can be used as the assignment target for a lambda expression or method reference. It represent an operation upon two operands of the same type,producing a result of the same type as the operands.
HighOrderFunction:These are functions that take other functions as parameters, or whose result is a function.
now we will see how to create a high order function from a bifunction and predicate
public class CreateHighOrderFunction { private static List list; private static List result = new ArrayList(); public CreateHighOrderFunction(List list) { CreateHighOrderFunction.list = list; } //find Even Number is a function which takes a Predicate public static List findEvenNumbers(Predicate predicateToApply) { BiFunction<List, Predicate, List> filter; //here filter is a BiFunction Which act as a high order Function it takes a list,another function predicate // as a argument filter = (listOfIntegers, predicate) -> { result.addAll(list.stream().filter(predicate).collect(Collectors.toList())); return result; }; //called the apply method of BiFunction and sent list and predicateToApply as argument filter.apply(list, predicateToApply); return result; }
here bifunction filter is applied as a high order function which takes the list of integers and a predicate which can be any filter
now we can test it using junit if you are not familar with it you can learn from here
in my case predicate is integerPredicate that i am using is to filter out the even numbers from the list so my expected list is List[2,4,6];
package test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; import main.CreateHighOrderFunction; import org.junit.BeforeClass; import org.junit.Test; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; public class HighOrderFunctionTest { @BeforeClass public static void setUp(){ CreateHighOrderFunction createHighOrderFunction = new CreateHighOrderFunction(Arrays.asList(1, 2, 3, 4, 5, 6)); } @Test public void testFindEvenNumbers() { //Predicate to filter out the list by even numbers Predicate integerPredicate = element -> element % 2 == 0; List expectedList = new ArrayList(); expectedList.add(2); expectedList.add(4); expectedList.add(6); List actualList = CreateHighOrderFunction.findEvenNumbers(integerPredicate); assertThat(expectedList, is(equalTo(actualList))); } }
in case you have any problem feel free to get the code from this repo
Reblogged this on Anurag-Knoldus.