Java 8 java.util.function.Function Tutorial with Examples
Tutorial explains the in-built functional interface
Since
Advantage of predefined
java.util.function.Function source code
Salient Points regarding Function<T, R>’s source code
OUTPUT of the above code
Explanation of above example's Code & Output
Usage of default method andThen() of Function
As explained earlier,
Lets see the example below which uses the same
OUTPUT of the above code
Explanation of above example's Code & Output
Lets see the example below which uses the same
OUTPUT of the above code
Explanation of above example's Code & Output
OUTPUT of the above code
Explanation of above example's Code & Output
Summary
In this tutorial we looked at what is the
Function<T, R> introduced in Java 8. It uses examples to show how the apply(), andThen(), compose() & identity() methods of the Function interface are to be used.
What is java.util.function.Function
Function<T, R> is an in-built functional interface introduced in Java 8 in the java.util.function package. The primary purpose for which Function<T, R> has been created is for mapping scenarios i.e when an object of a type is taken as input and it is converted(or mapped) to another type. Common usage of Function is in streams where-in the map function of a stream accepts an instance of Function to convert the stream of one type to a stream of another type.Since
Function<T, R> is a functional interfaceClick to Read tutorial on Functional Interfaces, hence it can be used as the assignment target for a lambda expressionClick to Read Lambda Expressions Tutorial or a method referenceClick to Read Tutorial on Java 8's Method References.
Function Descriptor of Function<T, R>
Function<T, R>’s Function Descriptor is T -> R. This means an object of type T is input to the lambda and an object of type R is obtained as return value. To understand Function Descriptors in details you can refer the function descriptor tutorialTutorial explaining function descriptors.Advantage of predefined
java.util.function.Function: In all scenarios where an object of a particular type is the input, an operation is performed on it and and object of another type is returned as output, the in-built functional interface Function<T, R> can be used without the need to define a new functional interface every time.
java.util.function.Function source code
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
Functioninterface has been defined with the generic typesT&R, whereTis the type of the input andRis the output type.- Method
apply()is the primary abstract functional method ofFunctioninterface. It takes as input a parametertof typeTand gives an output object of typeR. Function<T, R>has two default methods. First default methodcompose()combines the function on which it is applied(lets call it the current function) with another function, namedbefore, in such a way that when the combined function is applied then first thebeforefunction is applied which converts the input typeVto typeT. And then the current function converts this object of typeTto its output typeR. Thus, the combined function obtained as a result ofcompose()applies both the functions, in the process converting typeVtoR.- The second default method is
andThen()which combines the function on which it is applied(current function) with another function, namedafter, in such a way that when the combined function is called then first the current function is applied which converts the input typeTto typeR. And then theafterfunction is applied which converts from typeRtoV. Thus, the combined function obtained by usingandThen()default method applies both functions internally, in the process converting typeTto typeV. Function<T, R>also contains a static methodidentity()which is very simple as it returns as-is whatever is given to it as input. In the code above it takes as input a parametertof TypeTand returns back thist.
apply() method where it converts/maps from a list of Employee types to a list of Strings containing the names of all Employees. Let us now go through the code after which I will explain how the apply() method works - Java 8 code example showing usage of Function.apply() method
package com.javabrahman.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class FunctionTRExample{
public static void main(String args[]){
Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();};
List<Employee> employeeList=
Arrays.asList(new Employee("Tom Jones", 45),
new Employee("Harry Major", 25),
new Employee("Ethan Hardy", 65),
new Employee("Nancy Smith", 15),
new Employee("Deborah Sprightly", 29));
List<String> empNameList=convertEmpListToNamesList(employeeList, funcEmpToString);
empNameList.forEach(System.out::println);
}
public static List<String> convertEmpListToNamesList(List<Employee> employeeList, Function<Employee, String> funcEmpToString){
List<String> empNameList=new ArrayList<String>();
for(Employee emp:employeeList){
empNameList.add(funcEmpToString.apply(emp));
}
return empNameList;
}
}
Tom Jones Harry Major Ethan Hardy Nancy Smith Deborah Sprightly
funcEmpToStringis an instance ofFunction<Employee,String>. This is thejava.util.function.Functioninstance which is used to convert/map from anEmployeeobject to aStringvalue.- The lambda defining funcEmpToString is -
(Employee e)-> {return e.getName();}. It takes as input anEmployeeobject and returns his\her name, which is aStringvalue, as output. - The list of employees is passed to method
convertEmpListToNamesList()along with the Function objectfuncEmpToString; - The method
convertEmpListToNamesList()iterates over all the employees in the employee list, applies the functionfuncEmpToStringto each of theEmployeeobjects, getting back the employee names inStringformat, which it puts in a employee name list and sends it back to themain()method. - On printing the employee name list we get the names of all the employees as required.
andThen() default method combines the current Function instance with another one and returns a combined Function instance which applies the two functions in sequence with the function passed as parameter to andThen() being invoked after the current function.Lets see the example below which uses the same
funcEmpToString Function as used in the apply() method's usage example above and combines it with a initialFunction Function instance which maps\converts a String to its first letter - Java 8 code showing usage of default method Function.andThen()
//import statements are same as in apply() example
public class FunctionTRAndThenExample{
public static void main(String args[]){
Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();};
List<Employee> employeeList=
Arrays.asList(new Employee("Tom Jones", 45),
new Employee("Harry Major", 25),
new Employee("Ethan Hardy", 65),
new Employee("Nancy Smith", 15),
new Employee("Deborah Sprightly", 29));
Function<String,String> initialFunction= (String s)->s.substring(0,1);
List<String> empNameListInitials=convertEmpListToNamesList(employeeList, funcEmpToString.andThen(initialFunction));
empNameListInitials.forEach(str->{System.out.print(" "+str);});
}
public static List<String> convertEmpListToNamesList(List<Employee> employeeList, Function<Employee, String> funcEmpToString){
List<String> empNameList=new ArrayList<String>();
for(Employee emp:employeeList){
empNameList.add(funcEmpToString.apply(emp));
}
return empNameList;
}
}
T H E N D
- Function instance
funcEmpToStringmaps\converts anEmployeeobject to aStringof his\her name. - Function instance
initialFunctionmaps\converts aStringto its initial or first letter. - Default method
andThen()is used to combineinitialFunctionwithfuncEmpToString. What the combined method does is that it first maps anEmployeeto his\her name and then takes out the first letter from the name as aStringvalue. This combined function is passed asFunctionparameter toconvertEmpListToNamesList()method along with the employee list. - When the
convertEmpListToNamesList()applies the combined function to each of theEmployeeobjects, then the result is aStringlist first letters of names of each employee. - This is the required output i.e. T H E N D
compose() default method combines the current Function instance with another one and returns a combined Function instance which applies the two functions in sequence with the parameter function to compose() being invoked before the current function.Lets see the example below which uses the same
funcEmpToString Function as used in the apply() usage example and combines it with a funcEmpFirstName Function instance which converts the full-name of the employee object passed to it to just the first name of the employee - Java 8 code showing usage of default method Function.compose()
//import statements are same as in apply() example
public class FunctionTRComposeExample{
public static void main(String args[]){
Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();};
Function<Employee, Employee> funcEmpFirstName=
(Employee e)-> {int index= e.getName().indexOf(" ");
String firstName=e.getName().substring(0,index);
e.setName(firstName);
return e;};
List<Employee> employeeList=
Arrays.asList(new Employee("Tom Jones", 45),
new Employee("Harry Major", 25),
new Employee("Ethan Hardy", 65),
new Employee("Nancy Smith", 15),
new Employee("Deborah Sprightly", 29));
List<String> empFirstNameList= convertEmpListToNamesList(employeeList,funcEmpToString.compose(funcEmpFirstName));
empFirstNameList.forEach(str->{System.out.print(" "+str);});
}
public static List<String> convertEmpListToNamesList(List<Employee> employeeList, Function<Employee, String> funcEmpToString){
List<String> empNameList=new ArrayList<String>();
for(Employee emp:employeeList){
empNameList.add(funcEmpToString.apply(emp));
}
return empNameList;
}
}
Tom Harry Ethan Nancy Deborah
- Function instance
funcEmpToStringmaps\converts anEmployeeobject to aStringvalue of his\her name. - Function instance
funcEmpFirstNamemaps\converts thenameinside anEmployeeobject to the first name using thesubstringmethod ofString. - Default method
compose()is used to combinefuncEmpFirstNamewith funcEmpToStringString. What the combined method does is that it first converts the name of anEmployeeinto just his\her first name returning the sameEmployeeobject back with the changed value ofname. It then converts\maps theEmployeeobject to just itsnameas aString.This combined function is passed asFunction<Employee, String>parameter toconvertEmpListToNamesList()method along with the employee list. - When the
convertEmpListToNamesList()applies the combined function to each of theEmployeeobjects, then the result is the list of first names of each employee. - This is the required output i.e. Tom Harry Ethan Nancy Deborah
identity() is very simple - it just returns back the parameter which it gets as input. Lets see an example to see how identity() method works -
Java 8 code showing usage of static method Function.identity()
//import statements are same as in apply() example
public class FunctionTRIdentityExample{
public static void main(String args[]){
Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();};
List<Employee> employeeList=
Arrays.asList(new Employee("Tom Jones", 45),
new Employee("Harry Major", 25),
new Employee("Ethan Hardy", 65),
new Employee("Nancy Smith", 15),
new Employee("Deborah Sprightly", 29));
List<Employee> empNameListInitials=applyIdentityToEmpList(employeeList, Function.identity());
empNameListInitials.forEach(System.out::println);
}
public static List<Employee> applyIdentityToEmpList(List<Employee> employeeList, Function<Employee, Employee> funcEmpToEmp){
List<Employee> empNameList=new ArrayList<Employee>();
for(Employee emp:employeeList){
empNameList.add(funcEmpToEmp.apply(emp));
}
return empNameList;
}
}
Employee Name:Tom Jones Age:45 Employee Name:Harry Major Age:25 Employee Name:Ethan Hardy Age:65 Employee Name:Nancy Smith Age:15 Employee Name:Deborah Sprightly Age:29
- Function instance
funcEmpToStringmaps\converts anEmployeeobject to aStringof his\her name. - Employee list is passed to the method
applyIdentityToEmpList()along with an instance ofFunction.identity(). The parameter ofapplyIdentityToEmpList()which takesFunction.identity()value is nothing butFunction<Employee, Employee>i.e. an equivalent of aFunctionwhich takesEmployeeas input and gives back (the same)Employeeas output. - Method
applyIdentityToEmpList()takes the input employee list, iterates through it, applies theidentity()function to each employee in the list and returns back the list of employees obtained as a result of applying theidentity()function. - As we now know, the identity function does nothing, it just returns back the object it receives as input. So, what we get back is the same employee list which we passed to the
applyIdentityToEmpList()method! And the same i.e. original employee list is printed as output!! - This is how the static method
Function.identity()works.
Function<T, R> in-built interface defined in Java 8 and what are its advantages. We then looked at how to use the Function interface using its apply() method, default methods andThen() & compose(), and lastly how to use the static method identity().Functional Interface tutorials on JavaBrahman
Functional Interfaces in Java 8Tutorial on basics of Functional Interfaces in Java 8 Predicate Interface TutorialClick to read tutorial on Predicate Functional Interface Consumer Interface TutorialClick to read tutorial on Consumer Functional Interface Function Interface TutorialClick to read tutorial on Function Interface Supplier Interface TutorialClick to read tutorial on Supplier Functional Interface java.util.function package overviewClick to read overview of java.util.function package
Functional Interfaces in Java 8Tutorial on basics of Functional Interfaces in Java 8 Predicate Interface TutorialClick to read tutorial on Predicate Functional Interface Consumer Interface TutorialClick to read tutorial on Consumer Functional Interface Function Interface TutorialClick to read tutorial on Function Interface Supplier Interface TutorialClick to read tutorial on Supplier Functional Interface java.util.function package overviewClick to read overview of java.util.function package