In this article, we will discuss different variation of pre-defined BiFunction Functional Interface available for primitive data-types like int, long, double, etc. in Java 1.8 version
Primitive BiFunction Functional Interface (return-type) :
- This is very similar to BiFunction Functional Interface where it accepts 2 input argument of any data-type but return-type must be primitive-type like int, long and double, etc, whereas BiFunction allows to accept any data-type and return result in any data-type
- Performance-wise primitive BiFunction is much faster compared to Function<T, U, R>
- There are lot of conversion happening for auto-boxing & auto-unboxing for converting primitive-type to wrapper-type and again wrapper-type to primitive-type and so on
- To avoid unnecessary conversion between primitive-type to wrapper-type and vice-versa, primitive-type specific BiFunction Functional Interface for conversion introduced in Java 1.8 version as listed below,
- ToIntBiFunction
- ToLongBiFunction
- ToDoubleBiFunction
We will look into method signature along with example for each one of the above primitive BiFunction Functional Interface
1. ToIntBiFunction Functional Interface
- This primitive ToIntBiFunction Functional Interface always returns value in primitive-type int only and it is not required to declare while defining ToIntBiFunction (or lambda expression)
- But we have to provide 2 input arguments for ToIntBiFunction<T, U> and it can be any data-type depending upon our requirement
- Method signature: int applyAsInt(T t, U u);
package java.util.function;
@FunctionalInterface
public interface ToIntBiFunction<T, U> {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
int applyAsInt(T t, U u);
}
1.1 Example for ToIntBiFunction :
- Here, while defining lambda expression using ToIntBiFunction we haven’t specified any return-type like Integer, still compiler doesn’t complain and executed well
- And we have provided 2 input argument type as String & String for our requirement, as we are calculating to add length of both String and return result
package net.bench.resources.primitive.bifunction.example;
import java.util.function.ToIntBiFunction;
public class ToIntBiFunctionExample {
public static void main(String[] args) {
// lambda expression for ToIntBiFunction FI
ToIntBiFunction<String, String> tibf = (s1, s2) -> s1.length() + s2.length();
// test above lambda with different String
System.out.println("1. Length of Bench and Resources is \t= "
+ tibf.applyAsInt("Bench", "Resources"));
System.out.println("2. Length of Oracle and Java is \t= "
+ tibf.applyAsInt("Oracle", "Java"));
System.out.println("3. Length of Spring and Boot is \t= "
+ tibf.applyAsInt("Spring", "Boot"));
System.out.println("4. Length of IBM and Redhat is \t\t= "
+ tibf.applyAsInt("IBM", "Redhat"));
System.out.println("5. Length of Redhat and OpenShift is \t= "
+ tibf.applyAsInt("Redhat", "OpenShift"));
}
}
Output:
1. Length of Bench and Resources is = 14
2. Length of Oracle and Java is = 10
3. Length of Spring and Boot is = 10
4. Length of IBM and Redhat is = 9
5. Length of Redhat and OpenShift is = 15
2 ToLongBiFunction Functional Interface
- This primitive ToLongBiFunction Functional Interface always returns value in primitive-type long only and it is not required to declare while defining ToLongBiFunction (or lambda expression)
- But we have to provide 2 input argument for ToLongBiFunction<T, U> and it can be any data-type depending upon our requirement
- Method signature: long applyAsLong(T t, U u);
package java.util.function;
@FunctionalInterface
public interface ToLongBiFunction<T, U> {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
long applyAsLong(T t, U u);
}
2.1 Example for ToLongBiFunction :
- Here, while defining lambda expression using ToLongBiFunction we haven’t specified any return-type like Long, still compiler doesn’t complain and executed well
- And we have provided 2 input argument type as String, as we are converting into Long and then calculating average
package net.bench.resources.primitive.bifunction.example;
import java.util.function.ToLongBiFunction;
public class ToLongBiFunctionExample {
public static void main(String[] args) {
// lambda expression ToLongBiFunction FI
ToLongBiFunction<String, String> tlbf =
(s1, s2) -> (Long.parseLong(s1) + Long.parseLong(s2)) / 2;
// test above lambda with numbers
System.out.println("1. Average of 5 and 7 : "
+ tlbf.applyAsLong("5","7"));
System.out.println("2. Average of 25 and 75 : "
+ tlbf.applyAsLong("25","75"));
System.out.println("3. Average of 963 and 741 : "
+ tlbf.applyAsLong("963","741"));
System.out.println("4. Average of 1234 and 4312 : "
+ tlbf.applyAsLong("1234","4312"));
System.out.println("5. Average of 54872 and 65148 : "
+ tlbf.applyAsLong("54872","65148"));
System.out.println("6. Average of 102365 and 95685 : "
+ tlbf.applyAsLong("102365","956855"));
}
}
Output:
1. Average of 5 and 7 : 6
2. Average of 25 and 75 : 50
3. Average of 963 and 741 : 852
4. Average of 1234 and 4312 : 2773
5. Average of 54872 and 65148 : 60010
6. Average of 102365 and 95685 : 529610
3 ToDoubleBiFunction Functional Interface
- This primitive ToDoubleBiFunction Functional Interface always returns value in primitive-type double only and it is not required to declare while defining ToDoubleBiFunction (or lambda expression)
- But we have to provide 2 input argument for ToDoubleBiFunction<T, U> and it can be any data-type depending upon our requirement
- Method signature: double applyAsDouble(T t, U u);
package java.util.function;
@FunctionalInterface
public interface ToDoubleBiFunction<T, U> {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
double applyAsDouble(T t, U u);
}
3.1 Example for ToDoubleBiFunction :
- Here, while defining lambda expression using ToDoubleBiFunction we haven’t specified any return-type like Double, still compiler doesn’t complain and executed well
- And we have provided 2 input argument type as String, as we are converting into Double and then calculating average, so that it will return along with fractional part
package net.bench.resources.primitive.bifunction.example;
import java.util.function.ToDoubleBiFunction;
public class ToDoubleBiFunctionExample {
public static void main(String[] args) {
// lambda expression ToDoubleBiFunction FI
ToDoubleBiFunction<String, String> tdbf =
(s1, s2) -> (Double.parseDouble(s1) + Double.parseDouble(s2)) / 2;
// test above lambda with numbers
System.out.println("1. Average of 5 and 7 : "
+ tdbf.applyAsDouble("5","7"));
System.out.println("2. Average of 25 and 76 : "
+ tdbf.applyAsDouble("25","76"));
System.out.println("3. Average of 963 and 755 : "
+ tdbf.applyAsDouble("963","755"));
System.out.println("4. Average of 1235 and 4312 : "
+ tdbf.applyAsDouble("1235","4312"));
System.out.println("5. Average of 54871 and 65148 : "
+ tdbf.applyAsDouble("54871","65148"));
System.out.println("6. Average of 102365 and 95684 : "
+ tdbf.applyAsDouble("102365","956854"));
}
}
Output:
1. Average of 5 and 7 : 6.0
2. Average of 25 and 76 : 50.5
3. Average of 963 and 755 : 859.0
4. Average of 1235 and 4312 : 2773.5
5. Average of 54871 and 65148 : 60009.5
6. Average of 102365 and 95684 : 529609.5
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/function/ToIntBiFunction.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/ToLongBiFunction.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/ToDoubleBiFunction.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html
- https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Happy Coding !!
Happy Learning !!