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
Have Questions? Post them here!
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
- Eclipse like Auto Import Shortcut in Intelij IDE Android Studio - Android-Studio
- Resolve - zsh: command not found: code - zsh
- Setting Java_Home Environment variable on Windows Operating System - Java
- Access URL for SharePoint Tenant Admin Center (Online Office 365) - SharePoint
- [Android Studio] Button on click example - Android-Studio
- How to resolve Certificate Expired WhatsApp Error - WhatsApp
- How to see HTTP Request Response Headers in Google Chrome Browser - Chrome
- Java: Create Temporary Directory and File and Delete when application terminates - Java