In this post we’ll see examples of java.util.function.Function
functional interface.
R apply(T t)- Here T is the type of the argument passed to the method and it returns a value of type R.
If you are writing a Lambda expression that takes single argument of one type and returns a value of another type (or even same type) then that lambda expression can be written as an implementation of Function built-in functional interface.
Apart from the apply() abstract method Function() interface has following default and static methods.
- andThen(Function<? super R, ? extends V> after)- It is a default method which takes another Function as argument and returns a composed Function that performs, in sequence, first the operation of the calling Function followed by the after operation.
- compose(Function<? super V, ? extends T> before)- It is a default method in Function interface which takes another Function as argument and returns a composed Function that performs, in sequence, first the before operation then the operation of the calling Function.
- identity()- It is a static method that returns a function which returns its input argument.
Function interface apply() method example
1. Writing a function that returns the length of the passed string.
public class FunctionExample { public static void main(String[] args) { Function<String, Integer> function = (s) -> s.length(); System.out.println("Length- " + function.apply("Test")); System.out.println("Length- " + function.apply("Retrospective")); } }Output
Length- 4 Length- 13
In the example statement- Function<String, Integer> function = (s) -> s.length(); is the implementation of Function interface as a lambda expression.
When function.apply() method is called Java can infer from the context that lambda expression is the implementation of apply() method.
2. Finding factorial of a number as a lambda expression can be implemented as an instance of Function interface where input argument is an Integer and return value is of type Double.
import java.util.function.Function; public class FunctionExample { public static void main(String[] args) { double result = factorial(10); System.out.println("Factorial- " + result); } public static double factorial(int n) { Function<Integer, Double> function = x -> (x == 0) ? 1 : x * factorial(x - 1); return function.apply(n); } }Output
Factorial- 3628800.0
3. In this Function interface Java example a List of Employees is mapped to a list of names.
public class Employee { private String name; private String dept; private int salary; Employee(String name, String dept, int salary){ this.name = name; this.dept = dept; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } }
public class FunctionExample { public static void main(String[] args) { List<Employee> employeeList = new ArrayList<>(); employeeList.add(new Employee("Jack", "Finance", 5500)); employeeList.add(new Employee("Lisa", "Finance", 5600)); employeeList.add(new Employee("Scott", "Finance", 7000)); employeeList.add(new Employee("Nikita", "IT", 4500)); employeeList.add(new Employee("Tony", "IT", 8000)); List<String> nameList = mapTo(employeeList, (e)->e.getName()); nameList.forEach(System.out::println); } public static List<String> mapTo(List<Employee> empList, Function<Employee, String>mapper){ List<String> nameList = new ArrayList<>(); for(Employee emp : empList) { nameList.add(mapper.apply(emp)); } return nameList; } }Output
Jack Lisa Scott Nikita Tony
In the example implementation of the Function is passed as the method argument-
mapTo(employeeList, (e)->e.getName());
Function functional interface andThen() method example
In the example there will be two Function interfaces, on the first Function, andThen() is called with second Function as an argument. Now, when apply() method is called on the second Function interface, it first executes the first Function implementation and then the second one.
We’ll use the previous example of Employee list, in first function interface we’ll get the salary of the Employee and then in the second function increase it by 10%.
public class FunctionExample { public static void main(String[] args) { int fin, it; List<Employee> employeeList = new ArrayList<>(); employeeList.add(new Employee("Jack", "Finance", 5500)); employeeList.add(new Employee("Lisa", "Finance", 5600)); employeeList.add(new Employee("Scott", "Finance", 7000)); employeeList.add(new Employee("Nikita", "IT", 4500)); employeeList.add(new Employee("Tony", "IT", 8000)); Function<Employee, Integer> function = (e)->e.getSalary(); Function<Integer, Integer> functionAnother = (Integer i)-> i + ((i * 10)/100); List<Integer> salaryList = mapTo(employeeList, function.andThen(functionAnother)); salaryList.forEach(System.out::println); } public static List<Integer> mapTo(List<Employee> empList, Function<Employee, Integer>mapper){ List<Integer> salaryList = new ArrayList<>(); for(Employee emp : empList) { salaryList.add(mapper.apply(emp)); } return salaryList; } }Output
6050 6160 7700 4950 8800
Function functional interface compose() method example
compose() is reverse of andThen() first the Second Function interface implementation is called then the first one.
In the previous andThen() example if compose method is called on the second function and as argument first function interface is passed we’ll have the same result as the previous example.
List<Integer> salaryList = mapTo(employeeList, functionAnother.compose(function));
Function functional interface in JDK
These built-in functional interfaces are used extensively with in the JDK itself. A very good example of usage of Function interface is map() method of Stream interface in Java Stream API.
map(Function<? superT,? extends R>mapper)- Returns a stream consisting of the results of applying the given function to the elements of this stream.
The example where we mapped Employee object list to a List of employee names, can be written using map() method of Stream.
import java.util.ArrayList; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class FunctionExample { public static void main(String[] args) { int fin, it; List<Employee> employeeList = new ArrayList<>(); employeeList.add(new Employee("Jack", "Finance", 5500)); employeeList.add(new Employee("Lisa", "Finance", 5600)); employeeList.add(new Employee("Scott", "Finance", 7000)); employeeList.add(new Employee("Nikita", "IT", 4500)); employeeList.add(new Employee("Tony", "IT", 8000)); // map to name and then collect to a list List<String> nameList = employeeList.stream().map((e)->e.getName()).collect(Collectors.toList()); nameList.forEach(System.out::println); } }Output
Jack Lisa Scott Nikita Tony
That's all for the topic Function Functional Interface Java Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
- BiFunction Functional Interface Java Examples
- Supplier Functional Interface Java Examples
- Difference Between sleep() And wait() Methods in Java
- Java String repeat() Method
- Java Programs For Displaying Patterns
- Creating Password Protected Zip File in Java
- Spring Data JPA Example
- Method Overloading in Python With Examples
- React Props With Examples
No comments:
Post a Comment