Use of java.util.function Package
Use of Package java.util.function
This java.util.function package provides standard library-based functional interfaces for common requirements with their corresponding lambda expression, which can be used by the programmer in his code instead of creating brand new functional interfaces.
List of most commonly used Functional Interfaces under this package are:
1. Function
This interface has one function apply (), this function takes one input parameter as T and return value as R after performing some kind of operation on the input parameter.
Structure of Function interface:
@FunctionalInterface
public interface Function <T, R> {
R apply (T t);
}
This T and R may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by given example:
import java.util.function.Function;
public class Main {
public static void main (String args[])
{
// Function which takes in a number and returns half of it
Function<Integer, Double> half = a -> a / 2.0;
// apply the function to get the result
System.out.println(half.apply(10));
//One more use of it, to get square of an integer
Function<Integer, Integer> square= num1-> num1*num1;
System.out.println(“Square of number is =” +square.apply(4));
}
}
2. BiFunction
This interface also has one function apply (), this function takes two input parameters as T and U and returns a value as R after performing some kind of operation on given input parameters.
Structure of BiFunction interface:
@FunctionalInterface
public interface BiFunction <T, U, R> {
R apply (T t, U u);
}
This T, U, and R may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by given example:
import java.util.function.BiFunction;
public class Main {
public static void main (String args[])
{
// This function takes two numbers and returns sum of these two number
BiFunction<Integer, Integer, Integer> sum = (Integer num1, Integer num2) -> num1+num2;
// apply the function to get the result
System.out.println(“Sum of two number is =” +sum.apply(10,50));
}
}
3. UnaryOperator
This interface also has one function apply(), this function takes one input parameter and returns a value of the same input type after performing some kind of operation on the given input parameter.
Structure of Function interface:
@FunctionalInterface
public interface UnaryOperator <T> {
T apply (T t);
}
This T may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:
import java.util.function.*;
public class Main {
public static void main (String args[])
{
//This take one integer parameter and return square of an integer
UnaryOperator<Integer> square= num1-> num1*num1;
System.out.println(“Square of number is =” +square.apply(4));
}
}
4. Consumer
This interface has one function accept (), this function only takes one input parameter, but does not return any value.
Structure of Function interface:
@FunctionalInterface
public interface Consumer <T> {
void accept (T t);
}
This T may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:
import java.util.function.*;
public class Main {
public static void main (String args[])
{
// Function which takes in a number and returns half of it
Consumer<Integer> square = num1 -> {
System.out.println(“Square of number is =” +(num1*num1));
};
// accept function to get the result
square.accept(10);
}
}
5. BiConsumer
This interface has one function accept(), this function takes two input values for performing some operations and does not return any value.
Structure of Function interface:
@FunctionalInterface
public interface BiConsumer <T, R> {
void accept (T t, R r);
}
This T and R may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:
import java.util.function.*;
public class Main {
public static void main (String args [])
{
// This function takes two input value and does not return any value
BiConsumer<Integer, Integer> sum = (Integer num1, Integer num2) -> {
System.out.println(“Sum of two number are =” +(num1+num2));
};
//accept function to get the result
sum.accept(30,40);
}
}
6. Supplier
This interface has one function get(), this function does not take any parameter, but return value after performing a given task of the function.
Structure of Function interface:
@FunctionalInterface
public interface Supplier<T> {
T get ();
}
This T may have any type of value like Integer, Float, Double, String, etc.
Let’s understand the use of it by a given example:
import java.util.function.*;
public class Main {
public static void main (String args [])
{
// This function does not take any input value, but return value
Supplier<String> s1 = () -> {
System.out.println(“Enter your name=>”);
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
return name;
};
//get function to get the result
System.out.println(“Your name is =” +s1.get());
}
}