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!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- How to know your iPhone Model Name - iOS
- Notepad++ : Cannot load 64 or 32bit plugin Error. - NotepadPlusPlus
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED - Android
- Why I see Download pre-built shared indexes in IntelliJ - HowTos
- Android: programmatically turn Bluetooth on or off using Java code - Android
- How to show or hide columns in SharePoint Online List Library from - SharePoint
- Git Revision Questions Before the Interview - Git
- The Motivation Behind Generics in Java Programming - Java