12/12/2018

[Java8] BiFunction Functional Interface

From Wikipedia about Arity,

In logic, mathematics, and computer science, the arity /ˈærɪti/ (About this soundlisten) of a function or operation is the number of arguments or operands that the function takes

A binary function takes two arguments.
Example: {f(x,y)=2xy}

From Java definition

Represents a function that accepts two arguments and produces a result. This is the two arity specialization of Function.

here are some test cases to understand deeply biFunction.

   public void testFindEvenNumbers()
   {

      //given
      BiFunction<List<Integer>, Predicate<Integer>, List<Integer>> evenFinder = (list, even) -> {
         return list.stream().filter(even).collect(Collectors.toList());
      };

      //when
      List<Integer> evenNumList = evenFinder.apply(
         Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8),
         (num) -> num % 2 == 0
      );

      //verify
      MatcherAssert.assertThat(evenNumList, hasItems(2, 4, 6, 8));
   }

No comments:

Post a Comment