函数函数接口Java示例
在本文中,我们将看到java.util.function.Function函数接口的示例。
函数函数接口表示一种接受一个参数并产生结果的操作。该功能接口中的抽象方法是
R apply(T t)–此处T是传递给方法的参数的类型,它返回类型R的值。
如果编写的Lambda表达式采用一种类型的单个参数并返回另一种类型(甚至相同类型)的值,则可以将该Lambda表达式编写为Function内置函数接口的实现。
除了apply()抽象方法外,Function()接口还具有以下默认方法和静态方法。
- andThen(Function <?super R,?之后扩展V>)–这是一个默认方法,该方法将另一个Function用作参数,并返回一个组合的Function,该Function依次执行先执行调用Function的操作,然后执行after操作。
- compose(Function <?super V,?在之前扩展T>)–这是Function接口中的默认方法,该方法将另一个Function用作参数,并返回一个组合函数,该函数按顺序执行先执行before操作,然后执行调用的操作功能。
- identity()–这是一个静态方法,它返回一个函数,该函数返回其输入参数。
功能接口apply()方法示例
1.编写一个函数,该函数返回传递的字符串的长度。
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"));
}
}
输出:
Length- 4 Length- 13
在示例语句中Function <String,Integer> function =(s)-> s.length();是函数接口作为lambda表达式的实现。
调用function.apply()方法时,Java可以从上下文中推断出lambda表达式是apply()方法的实现。
2.查找数字的阶乘作为lambda表达式可以实现为Function接口的实例,其中输入参数是Integer,返回值是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);
}
}
输出:
Factorial- 3628800.0
3.在此函数接口Java示例中,"员工列表"被映射到名称列表。
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("Hyman", "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;
}
}
输出:
Hyman Lisa Scott Nikita Tony
在该示例的实现中,将方法实参作为方法参数传递给
mapTo(employeeList, (e)->e.getName());
函数功能接口和Then()方法示例
在该示例中,将在第一个Function上有两个Function接口,然后以第二个Function作为参数调用Then()。现在,在第二个Function接口上调用apply()方法时,它首先执行第一个Function实现,然后执行第二个。
我们将使用前面的"雇员"列表示例,在第一个功能界面中,我们将获取"雇员"的薪水,然后在第二个功能中,将其增加10%。
public class FunctionExample {
public static void main(String[] args) {
int fin, it;
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("Hyman", "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;
}
}
输出:
6050 6160 7700 4950 8800
函数功能接口compose()方法示例
compose()与andThen()相反,首先调用Second Function接口实现,然后是第一个。
在前面的andThen()示例中,如果在第二个函数上调用了compose方法,并且传递了第一个函数接口作为参数,我们将得到与上一个示例相同的结果。
List<Integer> salaryList = mapTo(employeeList, functionAnother.compose(function));
JDK中的功能功能接口
这些内置的功能接口已在JDK本身中广泛使用。使用功能接口的一个很好的例子是Java Stream API中Stream接口的map()方法。
map(Function <?superT,?extended R> mapper)–返回一个流,该流包括将给定函数应用于此流的元素的结果。
可以使用Stream的map()方法编写将Employee对象列表映射到员工姓名列表的示例。
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("Hyman", "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);
}
}
输出:
Hyman Lisa Scott Nikita Tony

