Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating a High Order Function From a BiFunction and Predicate

DZone's Guide to

Creating a High Order Function From a BiFunction and Predicate

High order functions take in other functions in as parameters or result in functions themselves. See how you can cobble them together with BiFunctions and predicates.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

In this blog, we will be reading about creating a high order function from a BiFunction and predicate in Java 8. Firstly a brief introduction to BiFunction, predicate, and high order functions:

Predicate: A predicate 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 the business layer and domain layer.

BiFunction: BiFunction is a functional interface that can be used as the assignment target for a lambda expression or method reference. It represents an operation upon two operands of the same type, producing a result of the same type as the operands.

High order function: 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, the 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're not familiar with JUnit, you can learn more here.

In my case, the predicate is integerPredicate. I'm using it to filter out the even numbers from the list so that 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)));

  }
}


And that's that! In case you have any problems, feel free to get the code from this repo and play around more with it yourself.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
lambda expressions ,bifunction ,java ,high order function ,functional interface ,predicate

Published at DZone with permission of Anurag Srivastava, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}