JDK8新特性 - 函数式编程

2021-06-16  本文已影响0人  家hao

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

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

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

@FunctionalInterface
public interface OperFunction<R,T> {
    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<Integer, Integer> of) {
        return of.operator(x, y);
    }

}

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

Consumer<T> : 消费型接口:有入参,无返回值
void accept(T t);

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

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

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

三、JDK8之函数式编程 Function

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}
public class FunctionObj implements Function {
    @Override
     public Object apply(Object o) {
         return o+"经过apply处理拼接上了";
     }
}
// 输出入参的10倍
Function<Integer, Integer> func = p -> p * 100; 
func.apply(100);

四、JDK8之函数式编程 BiFunction

@FunctionalInterface
public interface BiFunction<T, U, R> {
    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<Integer,Integer, Integer> bf) {
    return bf.apply(a, b);
}

五、JDK8之函数式编程 Consumer

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
}
public static void main(String[] args) throws Exception {
    Consumer<String> consumer = obj->{
    System.out.println(obj);
    System.out.println("调⽤短信接⼝发送短信,或者打印⽇志");
    sendMsg("8888888",consumer);
}

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

六、JDK8之函数式编程 Supplier

@FunctionalInterface
public interface Supplier<T> {
    T get();
}
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<Student> supplier = ()-> {
            Student student = new Student();
            student.setName("默认名称");
            return student;
        };
        return supplier.get();
    }
}

七、JDK8之函数式编程 Predicate

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
}
public static void main(String[] args) {
    List<String> list = Arrays.asList("awewrwe","vdssdsd","aoooo","psdddsd");
    List<String> results = filter(list,obj->obj.startsWith("a"));
    System.out.println(results);
}

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

猜你喜欢

热点阅读