函数式接口的提出是为了给Lambda表达式的使用提供更好的支持。
什么是函数式接口?
简单来说就是只定义了一个抽象方法的接口(Object类的public方法除外),就是函数式接口,并且还提供了注解:@FunctionalInterface
它们之间有哪些区别呢?
(1)、Consumer 《T》:消费型接口,有参无返回值
(2)、Supplier 《T》:供给型接口,无参有返回值
(3)、Function 《T,R》::函数式接口,有参有返回值
(4)、Predicate《T》: 断言型接口,有参有返回值,返回值是boolean类型
@Test
public void test(){
changeStr("hello",(str) -> System.out.println(str));
}
public void changeStr(String str, Consumer con){
con.accept(str);
}
@Test
public void test3(){
Long result = changeNum(100L, (x) -> x + 200L);
System.out.println(result);
}
public Long changeNum(Long num, Function fun){
return fun.apply(num);
}
@Test
public void test2(){
String value = getValue(() -> "hello");
System.out.println(value);
}
public String getValue(Supplier sup){
return sup.get();
}
public void test4(){
boolean result = changeBoolean("hello", (str) -> str.length() > 5);
System.out.println(result);
}
public boolean changeBoolean(String str, Predicate pre){
return pre.test(str);
}
在四大核心函数式接口基础上,还提供了诸如BiFunction、BinaryOperation、toIntFunction等扩展的函数式接口,都是在这四种函数式接口上扩展而来的,不做赘述。
总结:函数式接口的提出是为了让我们更加方便的使用lambda表达式,不需要自己再手动创建一个函数式接口,直接拿来用就好了。
/**
* 定义接口
*/
@FunctionalInterface
public interface Formula {
T calculate(F a);
default double sqrt(double number) {
return Math.sqrt(number);
}
}
/**
* 函数式接口调用
*/
public class Main {
public static void main(String args[]){
/**
* 普通方法实现
*/
Formula formula1=new Formula() {
@Override
public Double calculate(Double a) {
return sqrt(1*10);
}
};
double number1=formula1.calculate(100d);
double number2=formula1.sqrt(16);
System.out.println(number1);
System.out.println(number2);
/**
* 使用Lambda表达式来进行实现
*/
//访问局部变量,final可以不写,但是它隐式的是final,并且不可被修改
final int temp=3;
//Formula formula2=Double::valueOf;
Formula formula2=number->Double.valueOf(number)+3;
double num=formula2.calculate("1000");
System.out.println(num);
}
}
三种表现形式:
(1)、对象::实例方法名
(2)、类::静态方法名
(3)、类::实例方法名 (lambda参数列表中第一个参数是实例方法的调用 者,第二个参数是实例方法的参数时可用)
Consumer con = (x) -> System.out.println(x);
con.accept(100);
// 方法引用-对象::实例方法
Consumer con2 = System.out::println;
con2.accept(200);
// 方法引用-类名::静态方法名
BiFunction biFun = (x, y) -> Integer.compare(x, y);
BiFunction biFun2 = Integer::compare;
Integer result = biFun2.apply(100, 200);
// 方法引用-类名::实例方法名
BiFunction fun1 = (str1, str2) -> str1.equals(str2);
BiFunction fun2 = String::equals;
Boolean result2 = fun2.apply("hello", "world");
System.out.println(result2);
public class Person {
String fullName;
String firstName;
String lastName;
Person(String fullName) {
this.fullName=fullName;
}
Person() {}
Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
public interface PersonFactory {
P create(String firstName, String lastName);
}
public class Main {
public static void main(String args[]){
/**
* 构造器的实现以及原理
*/
PersonFactory personPersonFactory= Person::new;
personPersonFactory.create("zhang","zhaoliang");
// 构造方法引用 类名::new
Supplier sup = () -> new Person();
System.out.println(sup.get());
Supplier sup2 = Person::new;
System.out.println(sup2.get());
// 构造方法引用 类名::new (带一个参数)
Function fun = (f) -> new Person(f);
Function fun2 = Person::new;
System.out.println(fun2.apply("zhangzhaoliang"));
}
}
// 数组引用
Function fun = (x) -> new String[x];
Function fun2 = String[]::new;
String[] strArray = fun2.apply(10);
Arrays.stream(strArray).forEach(System.out::println);