Function接⼝
Lambda表达式必须先定义接⼝,创建相关⽅法之后才可使⽤,这样做⼗分不便,其实java8已经内置了许多接⼝, 例如下⾯四个功能型接⼝,所以⼀般很少会由⽤户去定义新的函数式接⼝
Java8的最⼤特性就是函数式接⼝,所有标注了@FunctionalInterface注解的接⼝都是函数式接⼝
Java8 内置的四⼤核⼼函数式接⼝
Consumer<T> : 消费型接⼝:有⼊参,⽆返回值
void accept(T t);
Supplier<T> : 供给型接⼝:⽆⼊参,有返回值
T get();
Function<T, R> : 函数型接⼝:有⼊参,有返回值
R apply(T t);
Predicate<T> : 断⾔型接⼝:有⼊参,有返回值,返回值类型确定是boolean
boolean test(T t);
Function接⼝使用
# 测试1
# 实现接口
public class FunctionObj implements Function {
@Override
public Object apply(Object o) {
return o+"经过apply处理拼接上了";
}
}
public class Main {
public static void main(String[] args) throws Exception {
test("小滴课堂",new FunctionObj());
}
public static void test(String input, Function function){
System.out.println( function.apply(input));
}
}
# 测试2
public class Main {
public static void main(String[] args) throws Exception {
// 测试2,写法2
Function<Integer,Integer> func = p->p*100;
System.out.println(func.apply(10));
}
}
BiFunction接⼝的使⽤
# BiFunction Function只能接收⼀个参数,如果要传递两个参数,则⽤ BiFunction
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}
import java.util.function.BiFunction;
public class Main {
public static void main(String[] args) throws Exception {
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);
}
}
Consumer接⼝的使⽤
Consumer 消费型接⼝:有⼊参,⽆返回值
将 T 作为输⼊,不返回任何内容
调⽤⽅法:void accept(T t);
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) throws Exception {
// 测试1
Consumer<String> consumer = obj->{
System.out.println(obj);
System.out.println("调用短信接口发送短信,或者打印日志");
};
sendMsg("8888888",consumer);
// 测试1,写法2
sendMsg("8888888",obj->{
System.out.println(obj);
System.out.println("调⽤短信接⼝发送短信,或者打印⽇志");
});
// 测试2
List<String> list = Arrays.asList("aaa","bbb");
sendMsg2(list,obj->{
System.out.println(obj);
System.out.println("调用短信接口发送短信,或者打印日志");
});
}
public static void sendMsg2(List<String> phone,Consumer<List<String>> consumer){
consumer.accept(phone);
}
public static void sendMsg(String phone,Consumer<String> consumer){
consumer.accept(phone);
}
}
Supplier接⼝的使⽤
Supplier: 供给型接⼝:⽆⼊参,有返回值
T:出参类型;没有⼊参
调⽤⽅法:T get();
@FunctionalInterface
public interface Supplier<T> {
T get();
}
public class Main {
public static void main(String[] args) throws Exception {
// 方式1
//Student student = new Student();
// 方式2
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();
}
}
class Student{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Predicate接⼝的使⽤
Predicate: 断⾔型接⼝:有⼊参,有返回值,返回值类型确定是boolean
T:⼊参类型;出参类型是Boolean
调⽤⽅法:boolean test(T t);
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
public class Main {
public static void main(String[] args) throws Exception {
List<String> list = Arrays.asList("asdfdsfdsfds","xcvsdfsdf","sdfsdf","awwa","ad4432","dsfsdf");
// 过滤出包含a的
List<String> results = filter(list,obj->obj.startsWith("a"));
// 打印出所有
System.out.println(list);
// 打印出过滤后的
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;
}
}