Bifunction in java 8
A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function package contains it.
Let us have some knowledge about the Functional interfaces.
Functional interfaces
Functional interfaces are interfaces that only have one abstract method. There can be as many default static methods as needed, but there can only be one abstract method. Additionally, object class methods may be declared.
Single Abstract Method Interfaces, or SAM Interfaces, are another name for functional interfaces. It is a newer version in Java that aids in the development of functional programming techniques.
@FunctionalInterface
public interface BiFunction<T,U,R>
The three type-parameters that the interface accepts are as follows:
T: It stands for the function's initial argument.
U: It stands for the function's second argument.
R: It stands for the function's outcome.
The description that may be expressed as (T, U)->R consists of T, U, and R.
Methods of BiFunction Interface
The interface offers the two techniques listed below:
- apply()
- andThen()
The BiFunctional apply() method
The apply() method applies the specified inputs to the operation and returns the function's outcome.
its syntax is:
R apply(T t, U u)
The Parameters:
t: It stands in for the initial function parameter.
u: The letter "u" stands for the second function parameter.
Returns:
The function's output is returned.
Example1:
Let the name of the file is BiFunctionApplyDemo.java
import java.util.function.BiFunction;
import java.util.function.Function;
public class BiFunctionApplyDemo.java
{
public static void main(String args[])
{
BiFunction <Integer, Integer, Integer> bkobj = (s, h) -> s + h;
// applying the method apply()
System.out.println("The sum of the s and the h is: " + bkobj.apply(47, 22));
}
}
Output:
The sum of the s and the h is: 69
Method using a BiFunction and Then()
A composite function is the result. It indicates that the method applies the before function first, followed by the after function. The operator of the combined function is informed of any exceptions that either function's evaluation throws.
Syntax:
default <V> BiFunction<T,U,V> andThen(Function<? super X,? extends S> later)
The Type-Parameter:
V: This letter stands for the output type of both the composed function and the after() function.
Parameters are:
after: It is applicable following this operation.
Returns:
A composite function is the result. It indicates that the method applies the before function first, followed by the after function.
If after is null, the procedure raises a NullPointerException.
Example1:
BiFunctionAndThenDemo.java
import java.util.function.BiFunction;
import java.util.function.Function;
public class BiFunctionAndThenExample
{
public static void main(String args[])
{
// carries out multiplication
BiFunction <Integer, Integer, Integer> bsobj = (x, y) -> x * y;
// carries out division
Function <Integer, Integer> sobj = (k) -> k / 2;
// functional composition
System.out.println("The outcome is: " + bsobj.andThen(kobj).apply(10, 10));
}
}
Output:
The outcome is: 50
Let's use a Java application to implement the two aforementioned techniques to better understand how the BiFunction interface functions.
Example for Bifunction
BiFunctionDemo2.java
import java.util.function.BiFunction;
import java.util.function.Function;
public class BiFunctionDemo2
{
public static void main(String args[])
{
// a simple use of apply()
BiFunction<Integer, Integer, Integer> f3 = (x, y) -> x + y;
System.out.println(f1.apply(42, 27)); //it returns and also print 69
// a simple andThen() illustration
Function<Integer, Integer> f9 = x -> x * x;
System.out.println(f3.andThen(f9).apply(31, 13));
}
}
Output:
69
1936
We can see from the above program, the statement in the above that is System.out.println(f3.andThen(f9).apply(2, 3)); It can be simply written as show below:
Integer r3 = f3.apply(31, 13);
Integer r9 = f9.apply(r3);
System.out.println(r9); //It returns and also print 1936
Let us have a look at another example program.
BiFunctionDemo9.java
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.*;
public class BiFunctionDemo9
{
public static void main(String args[])
{
// the creation of a Map instance
Map<Integer, String> map = new HashMap<>();
// updating the Map with values
map.put(10, "Janardhan");
map.put(11, "Hymavathi");
map.put(12, "Hemanth");
map.put(13, "Swetha");
map.put(14, "Archana");
map.put(15, "Puppy");
map.put(16, "Bandu");
// To get the quantity of occurrences, use Collections.frequency
BiFunction<Integer, String, String> s = (key, value) -> "[Key="+key+", "+value+"("+Collections.frequency(map.values(), value)+")]";
// traverse the Map iteratively
map.forEach((h,p)-> System.out.println(f.apply(h, p)));
}
}
Output:
[Key=10, Janardhan(10)]
[Key=11, Hymavathi(11)]
[Key=12, Hemanth(10)]
[Key=13, Swetha(10)]
[Key=14, Archana(10)]
[Key=15, Puppy(10)]
[Key=16, Bandu(11)]