name | type | description |
---|---|---|
Consumer | Consumer< T > | 接收T對象,不返回值 |
Predicate | Predicate< T > | 接收T對象並返回boolean |
Function | Function< T, R > | 接收T對象,返回R對象 |
Supplier | Supplier< T > | 提供T對象(例如工廠),不接收值 |
UnaryOperator | UnaryOperator< T > | 接收T對象,返回T對象 |
BiConsumer | BiConsumer<T, U> | 接收T對象和U對象,不返回值 |
BiPredicate | BiPredicate<T, U> | 接收T對象和U對象,返回boolean |
BiFunction | BiFunction<T, U, R> | 接收T對象和U對象,返回R對象 |
BinaryOperator | BinaryOperator< T > | 接收兩個T對象,返回T對象 |
參考:https://blog.csdn.net/huo065000/article/details/78964382java
PASS:Javascript可以將函數傳遞給另外一個函數,這應該算是函數式編程的一個體現,java的function包中的類也是相似的。編程
public interface Iterable<T> { default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } }
public class ConsumerTest { public static void main(String[] args) { ArrayList<Employee> employees = new ArrayList<>(); String[] prefix = {"A", "B"}; for (int i = 1; i <= 10; i++) employees.add(new Employee(prefix[i % 2] + i, i * 1000)); employees.forEach(new SalaryConsumer()); employees.forEach(new NameConsumer()); } static class Employee { private String name; private int salary; public Employee() { this.salary = 4000; } public Employee(String name, int salary) { this.name = name; 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; } @Override public String toString() { return new StringBuilder() .append("name:").append(name) .append(",salary:").append(salary) .toString(); } } // 輸出須要交稅的員工 static class SalaryConsumer implements Consumer<Employee> { @Override public void accept(Employee employee) { if (employee.getSalary() > 2000) { System.out.println(employee.getName() + "要交稅了."); } } } // 輸出須要名字前綴是‘A’的員工信息 static class NameConsumer implements Consumer<Employee> { @Override public void accept(Employee employee) { if (employee.getName().startsWith("A")) { System.out.println(employee.getName() + " salary is " + employee.getSalary()); } } } }
ArrayList的removeIf(Predicate):刪除符合條件的元素數組
若是條件硬編碼在ArrayList中,它將提供無數的實現,可是若是讓調用者傳入條件,這樣ArrayList就能夠從複雜和沒法猜想的業務中解放出來。app
// employee.getSalary() > 2000 提取成一個條件類 class SalaryConsumer implements Consumer<Employee> { @Override public void accept(Employee employee) { // 自行傳入本地的最低交稅工資 if (new SalaryPredicate(2000).test(employee)) { System.out.println(employee.getName() + "要交稅了."); } } } class SalaryPredicate implements Predicate<Employee>{ private int tax; public SalaryPredicate(int tax) { this.tax = tax; } @Override public boolean test(Employee employee) { return employee.getSalary() > tax; } }
public static void main(String[] args) { ArrayList<Employee> employees = new ArrayList<>(); String[] prefix = {"B", "A"}; for (int i = 1; i <= 10; i++) employees.add(new Employee(prefix[i % 2] + i, i * 1000)); int[] expenses = ListToArray(employees, new EmployeeToExpenses());// 公司對單個員工的支出數組 int[] incomes = ListToArray(employees, new EmployeeToIncome()); // 單個員工的收入數組 System.out.println("社保+公積金+稅=" + (sum(expenses) - sum(incomes)) + "元"); } private static int[] ListToArray(List<Employee> list, Function<Employee, Integer> function) { int[] ints = new int[list.size()]; for (int i = 0; i < ints.length; i++) ints[i] = function.apply(list.get(i)); return ints; } private static int sum(int[] salarys) { int sum = 0; for (int i = 0; i < salarys.length; i++) sum += salarys[i]; return sum; } // 公司支出 static class EmployeeToExpenses implements Function<Employee, Integer> { @Override public Integer apply(Employee employee) { // 假設公司公積金和社保爲工資的20% return Double.valueOf(employee.getSalary() * (1 + 0.2)).intValue(); } } // 員工實際到手工資 static class EmployeeToIncome implements Function<Employee, Integer> { @Override public Integer apply(Employee employee) { // 假設員工薪水 * 80% 爲到手工資 return Double.valueOf(employee.getSalary() * (1 - 0.2)).intValue(); } }
public static void main(String[] args) { // 生成固定工資的員工 Supplier<Employee> supplier = () -> new Employee(); Employee employee1 = supplier.get(); employee1.setName("test1"); Employee employee2 = supplier.get(); employee2.setName("test2"); System.out.println("employee1:" + employee1); System.out.println("employee2:" + employee2); }
public static void main(String[] args) { ArrayList<Employee> employees = new ArrayList<>(); String[] prefix = {"B", "A"}; for (int i = 1; i <= 10; i++) employees.add(new Employee(prefix[i % 2] + i, i * 1000)); System.o ut.println("公司進行薪資調整..."); salaryAdjustment(employees,new SalaryAdjustment(4000)); employees.forEach(System.out::println); } static void salaryAdjustment(List<Employee> list, UnaryOperator<Employee> operator) { for (int i = 0; i < list.size(); i++) { list.set(i, operator.apply(list.get(i))); } } static class SalaryAdjustment implements UnaryOperator<Employee> { private int salary; public SalaryAdjustment(int salary) { this.salary = salary; } @Override public Employee apply(Employee employee) { employee.setSalary(salary); return employee; } }