lambada表达式是替换匿名内部类的函数式接口实现类
函数式接口 只有一个抽象方法的接口(可以有默认方法和静态方法),通常带有@FunctionalInterface注解
常用函数式接口
-
Predicate / ˈpredɪkət/ 断言型函数式接口
@FunctionalInterface public interface Predicate<T> { /** * ——接收`T`对象并返回`boolean` */ boolean test(T t); }
-
Consumer 消费型函数式接口
@FunctionalInterface public interface Consumer<T> { /** * 接收T对象,不返回值 */ void accept(T t); }
-
Function<T, R>
功能型函数式接口@FunctionalInterface public interface Function<T, R> { /** * ——接收`T`对象,返回`R`对象 */ R apply(T t); }
-
Supplier<T>
供应型函数式接口@FunctionalInterface public interface Supplier<T> { /** * ——返回`T`对象(例如工厂),不接收值 */ T get(); }
我们只看接口的方法
函数式接口 | 入参 | 返回对象 |
---|---|---|
供给型函数式接口 T get(); | 无 | T |
消费型函数式接口 void accept(T t); | T | 无 |
断言型函数式接口 boolean test(T t); | T | boolean |
功能型函数式接口 R apply(T t); | T | R |
从上来看,断言型接口是功能型接口的子集。断言型接口是函数式接口的一种。
供给型接口示例
class StringSupplier{
public static String get() {
return "我很好";
}
public String get2() {
return "我很好2";
}
}
/**
* public interface Supplier<T> {
* T get();
*}
**/
public class Supplier1 {
public static void main(String[] args) {
//匿名内部类写法
Supplier c = new Supplier() {
@Override
public Object get() {
return "我很好!";
}
};
System.out.println("c.get() = " + c.get());//c.get() = 我很好!
//lambada 写法
//ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
Supplier c2 = ()->{
return "我很好!";
};
System.out.println("c2.get() = " + c2.get());//c2.get() = 我很好!
//方法引用 如果方法的入参和返回值类型一样可以用方法引用。
//Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
// 类::静态方法引用
Supplier s = StringSupplier::get;
System.out.println("s.get() = " + s.get());//s.get() = 我很好
// 对象::普通方法引用
Supplier s2 = new StringSupplier()::get2;
System.out.println("s2.get() = " + s2.get());//s2.get() = 我很好2
//类::new
Supplier constructor=StringSupplier::new;//引用构造方法
StringSupplier o = (StringSupplier)constructor.get();//调用构造方法,产生对象
System.out.println("constructor = " + constructor.get());//constructor = com.axa.aam.service.StringSupplier@254989ff
}
}
消费型函数式接口示例
class StringConsumer<String>{
public void get(String s){
System.out.println("s = " + s);
}
}
/**
* public interface Consumer<T> {
* void accept(T t);
*}
**/
public class Consumer2 {
public static void main(String[] args) {
//匿名内部类写法
Consumer c = new Consumer<String>() {
@Override
public void accept(String o) {
System.out.println("o = " + o);
}
};
c.accept("我很好!");// c.get() = 我很好!
//lambada 写法
//ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
Consumer c2 = (o)->{
System.out.println("o = " + o);
};
c2.accept("我很好!"); //c2.get() = 我很好!
//方法引用 如果方法的入参和返回值类型一样可以用方法引用。
//Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
// 对象::普通方法引用
Consumer s2 = new StringConsumer()::get;
s2.accept("我很好?");
}
}
断言型函数式接口示例
class StringPredicate<String>{
public boolean get(String s){
return s==null;
}
}
/**
* public interface Predicate<T> {
*
* boolean test(T t);
*}
**/
public class Predicate3 {
public static void main(String[] args) {
//匿名内部类写法
Predicate c = new Predicate<String>() {
@Override
public boolean test(String o) {
return o==null;
}
};
System.out.println(c.test("我很好!"));// c.test() = false
//lambada 写法
//ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
Predicate c2 = (o)->{
return o==null;
};
System.out.println(c2.test("我很好!")); //c2.test() = false
//方法引用 如果方法的入参和返回值类型一样可以用方法引用。
//Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
// 对象::普通方法引用
Predicate s2 = new StringPredicate()::get;
System.out.println(s2.test("我很好?")); //s2.test() false
}
功能型函数式接口示例
class StringFunction<Integer,String>{
public java.lang.String get(Integer i){
return i.toString();
}
public StringFunction(Integer q) {
System.out.println("我是构造方法");
}
public StringFunction() {
}
}
/**
* public interface Function<T, R> {
* R apply(T t);
*}
**/
public class Function4 {
public static void main(String[] args) {
//匿名内部类写法
Function c=new Function<Integer,String>() {
@Override
public String apply(Integer o) {
return o.toString();
}
};
System.out.println(c.apply(5));// c.apply() = 5
//lambada 写法
//ambda表达式的语法由方法 参数列表、箭头符号->和函数体组成。
Function c2 = (o)->{
return o.toString();
};
System.out.println(c2.apply(6)); //c2.apply() = 6
//方法引用 如果方法的入参和返回值类型一样可以用方法引用。
//Lambda表达式让我们自己重新写方法实现,方法引用是使用已经存在的实现。
// 对象::普通方法引用
Function s2 = new StringFunction()::get;
System.out.println(s2.apply(7)); //s2.apply() 7
Comparator<String> stringComparator = String::compareTo;
//类::new 构造器引用
Function<Integer, StringFunction> s3 = StringFunction::new; //引用构造方法
StringFunction apply = s3.apply(8); //调用构造方法,产生对象
System.out.println("apply = " + apply);//apply = com.axa.aam.service.StringFunction@179d3b25
}
}