Java 8 introduced Function Interfaces, Lambda Expressions, and also a lot many utility functions to make use with Lambda's and Streams, you make find these been used very much in all new classes and methods added to Java 8 and later.
In this tutorial, we will take a look at the java.util.Function and java.util.BiFunction Functional Inteface classes,
Java Functional Interface Function
If you go to the Java rt.jar and look at the java.util package you will see the Function.java
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
T – This is to represent the type of input to the function.
R – This is to represent what Type is the return type of the function.
Example 1: Function that takes a String name and returns a String Greeting
public static void main(String[] args) {
Function<String,String> greeting = (name) -> "Hello "+name+"!";
System.out.println(greeting.apply("Sam"));
}
Output:
Hello Sam!
Example 2: Function that takes a Integer and returns a Boolean true if a Even else false
public static void main(String[] args) {
Function<Integer,Boolean> evenOdd = (number) -> {
if (number % 2 == 0) {
return true;
} else {
return false;
}
};
System.out.println(evenOdd.apply(5));
System.out.println(evenOdd.apply(4));
}
Output:
false
true
Java Functional Interface BiFunction
Again, go the java.util package you will see the BiFunction.java
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}
T – This is to represent the Type of the first input to the function.
U – This is to represent the Type of the second input to the function.
R – This is to represent what Type is the return type of the function.
Example 1: BiFunction that takes in two Integers and returns theirs Sum
BiFunction<Integer,Integer,Integer> getSum = (n1,n2) -> n1 + n2;
System.out.println(getSum.apply(10,20));
Output:
30
Example 2: BiFunction that takes in two Integers and returns theirs multiplied calue
BiFunction<Integer,Integer,Integer> getSum = (n1,n2) -> n1 * n2;
System.out.println(getSum.apply(10,20));
Output:
200
Facing issues? Have Questions? Post them here! I am happy to answer!
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
- Disabling Spell Check in Android Studio - Android-Studio
- How to enable, create and use Virtual Breakout Rooms in Microsoft Teams - Teams
- Bash Script wait for user Input Command - Bash
- Jupyter Notebook: 500 Internal Server Error - nbconvert failed: xelatex not found on PATH - Python
- Convert String into DateTime in Python - Python
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations - Android
- [Fix] Powershell - Term winget is not recognized as the name of a cmdlet function or script file - Powershell
- How to use a different Python version with virtualenv - Python