JDK8新特性 - 函数式编程

一、JDK8之自定义函数式编程

1.使用Lambda表达式,自定义lambda接口编程

  • 定义⼀个函数式接口 需要标注此接口 @FunctionalInterface,否则万⼀团队成员在接口上加了其他方法则容易出故障
  • 编写一个方法,输入需要操做的数据和接口
  • 在调用方法时传⼊数据 和 lambda 表达式,用来操作数据

2.定义⼀个可以使用加减乘除的接口以前需要定义4个方法

  • 使⽤Lambda表达式后
@FunctionalInterface
public interface OperFunction {
    R operator(T t1, T t2);
}

public class Main {
    
    public static void main(String[] args) throws Exception {
        System.out.println(operator(20, 5, (Integer x, Integer y) -> {
            return x * y;
        }));
        System.out.println(operator(20, 5, (x, y) -> x + y));
        System.out.println(operator(20, 5, (x, y) -> x - y));
        System.out.println(operator(20, 5, (x, y) -> x / y));
    }


    public static Integer operator(Integer x, Integer y, OperFunction of) {
        return of.operator(x, y);
    }

}

二、JDK8里面的函数式编程--四个功能型接口

  • Lambda表达式必须先定义接口,创建相关方法之后才可使用,这样做十分不便,其实java8已经内置了许多接口, 例如下面四个功能型接口,所以⼀般很少会由用户去定义新的函数式接口
  • Java8的最大特性就是函数式接口,所有标注了@FunctionalInterface注解的接口都是函数式接口
Consumer : 消费型接口:有入参,无返回值
void accept(T t);

Supplier : 供给型接⼝:无入参,有返回值
T get();

Function : 函数型接口:有入参,有返回值
R apply(T t);

Predicate : 断⾔型接口:有入参,有返回值,返回值类型确定是boolean
boolean test(T t);

三、JDK8之函数式编程 Function

  • Function
    1. 传入一个值经过函数的计算返回另⼀个值
    2. T:入参类型,R:出参类型
    3. 调用方法:R apply(T t)
@FunctionalInterface
public interface Function {
    R apply(T t);
}
  • 作用:将转换逻辑提取出来,解耦合
    不要看过于复杂,就是⼀个接口,下⾯是自定义实现
public class FunctionObj implements Function {
    @Override
     public Object apply(Object o) {
         return o+"经过apply处理拼接上了";
     }
}
  • 常规使用案例
// 输出入参的10倍
Function func = p -> p * 100; 
func.apply(100);

四、JDK8之函数式编程 BiFunction

  • BiFunction Function只能接收一个参数,如果要传递两个参数,则用 BiFunction
@FunctionalInterface
public interface BiFunction {
    R apply(T t, U u);
}
  • 常规使用案例
public static void main(String[] args) {
     System.out.println(operator(10,21,(a,b)->a+b));
     System.out.println(operator(10,2,(a,b)->a-b));
     System.out.println(operator(8,4,(a,b)->a*b));
     System.out.println(operator(10,2,(a,b)->a/b));
 }

public static Integer operator(Integer a, Integer b, BiFunction bf) {
    return bf.apply(a, b);
}

五、JDK8之函数式编程 Consumer

  • Consumer
    1. Consumer 消费型接口:有入参,无返回值
    2. 将 T 作为输⼊,不返回任何内容
    3. 调用方法:void accept(T t);
@FunctionalInterface
public interface Consumer {
    void accept(T t);
}
  • 常规使用案例
    用途:因为没有出参,常用于打印、发送短信等消费动作
public static void main(String[] args) throws Exception {
    Consumer consumer = obj->{
    System.out.println(obj);
    System.out.println("调⽤短信接⼝发送短信,或者打印⽇志");
    sendMsg("8888888",consumer);
}

public static void sendMsg(String phone,Consumer consumer){
    consumer.accept(phone);
}

六、JDK8之函数式编程 Supplier

  • Supplier
    1. Supplier: 供给型接口:无入参,有返回值
    2. T:出参类型;没有入参
    3. 调用方法:T get();
@FunctionalInterface
public interface Supplier {
    T get();
}
  • 常规使用案例
    用途: 泛型⼀定和方法的返回值类型是⼀种类型,如果需要获得⼀个数据,并且不需要传入参数,可
    以使用Supplier接⼝,例如无参的工厂方法,即工厂设计模式创建对象,简单来说就是提供者
class Student{
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class Test{
    public static void main(String[] args) {
        Student student = newStudent();
        System.out.println(student.getName());
    }
    public static Student newStudent(){
        Supplier supplier = ()-> {
            Student student = new Student();
            student.setName("默认名称");
            return student;
        };
        return supplier.get();
    }
}

七、JDK8之函数式编程 Predicate

  • Predicate
    1. Predicate: 断⾔型接口:有入参,有返回值,返回值类型确定是boolean
    2. T:入参类型;出参类型是Boolean
    3. 调⽤方法:boolean test(T t);
@FunctionalInterface
public interface Predicate {
    boolean test(T t);
}
  • 常规使用案例
    用途: 接收⼀个参数,用于判断是否满足⼀定的条件,过滤数据
public static void main(String[] args) {
    List list = Arrays.asList("awewrwe","vdssdsd","aoooo","psdddsd");
    List results = filter(list,obj->obj.startsWith("a"));
    System.out.println(results);
}

public static List filter(List list,Predicate predicate) {
    List results = new ArrayList<>();
    for (String str : list) {
        if (predicate.test(str)) {
            results.add(str);
        }
    }
    return results;
}

你可能感兴趣的:(JDK8新特性 - 函数式编程)