Java 8 | BiFunction | BiPredicate | BiConsumer
2 min readJan 2, 2021
BiFunction
- The lambda expression assigned to an object of BiFunction type is used to define its apply() which eventually applies the given function on the arguments.
R apply(T t, U u);
- The main advantage of using a BiFunction is that it allows us to use 2 input arguments while in function we can only have 1 input argument.
@FunctionalInterface
public interface BiFunction<T, U, R>
- T — Type of the first argument to the function.
- U — Type of the second argument to the function.
- R — Type of the result of the function.
- 10. lines, we defined BiFunction with concat method.
- 12.lines, we used apply method and put values.
- 18.lines, we call powToString method, the method expects 2 integer values and a biFunction and a Function.
- The method takes values and does Math.pow and then convert the result to String.
BiConsumer
- BiConsumers are useful when it is not required to return any value as they are expected to operate via side-effects.
- We use accept method of BiConsumer
void accept(T t, U u)
- BiConsumers is that it allows us to use 2 input arguments while in function don’t return any value
@FunctionalInterface
public interface BiConsumer<T, U>
- T — Type of the first argument to the function.
- U — Type of the second argument to the function.
- 10. lines, we defined BiConsumer with map
- 15–18.lines we put some values in map while we called putMap method
BiPredicate
- BiPredicate is a functional interface, which accepts two arguments and returns a boolean for the test
boolean test(T t, U u);
- T — Type of the first argument to the function.
- U — Type of the second argument to the function.
@FunctionalInterface
public interface BiPredicate<T, U> {
- 7.lines we defined BiPredicate,
- we put 2 arguments while called filterBiPredicate method and we used test(T t, U u) method,
Thank you for your time :)
Github: Binary
References:
BiFunction
BiConsumer
BiPredicate