1.Lambda表达式
Lambd表达式可以理解为一种可传递的匿名函数。它没有名称,但有参数列表、函数主体、返回类型,还有可能会抛出异常。在package java.util中有一个接口,接着创建苹果类:
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
...
}
public class Apple {
private int weight;
private String color;
public Apple(int weight, String color) {
this.weight = weight;
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
匿名类的表达形式:
Comparator<Apple> appleComparator = new Comparator<Apple>() {
@Override
public int compare(Apple o1, Apple o2) {
return o1.getWeight()-o2.getWeight();
}
};
Lambda表达式形式:
Comparator<Apple> appleComparator1 = (Apple apple1, Apple apple2)->
apple1.getWeight()-apple1.getWeight();
可以看到Lambda表达式更简单:
1. ()->{} 无参,{}做处理;
2.()->retrun 值; 无参,返回值;
3.(String s)->s 有参,返回值;
4.(String s)->{} 有参,做处理。
2.函数式接口
只定义一个抽象方法的接口。它就是函数式接口。@FuntionallInterface注解,表示会设计成一个函数接口,不符合规范,会编译报错。
jdk8 自定义了简单的四个函数式接口:
@FunctionalInterface
public interface Predicate<T> {
//接收一个参数t,返回boolean
boolean test(T t);
//创建predicate实例, predicate.and(other),
结果为 predicate.test(t) and other.test(t)
default Predicate<T> and(Predicate<? super T> other);
//创建predicate实例, predicate.or(other),
结果为 predicate.test(t) or other.test(t)
default Predicate<T> or(Predicate<? super T> other);
//创建predicate实例, predicate.negate()
结果为 !predicate.test(t)
default Predicate<T> negate()
// Predicate.isEuqal("true").test("true")
static <T> Predicate<T> isEqual(Object targetRef)
}
对应的测试结果:
Predicate<String> predicate1=s->Boolean.parseBoolean(s);
System.out.println(predicate1.test("true")); //true
System.out.println(predicate1.and(s -> Boolean.parseBoolean(s)).test("false"));//false
System.out.println(predicate1.or(s -> Boolean.parseBoolean(s)).test("false"));//false
System.out.println(predicate1.negate().test("true"));//false
System.out.println(Predicate.isEqual("true").test("true"));//true
@FunctionalInterface
public interface Consumer<T> {
//接受一个参数t
void accept(T t);
//创建一个consumer实例, consumer.andThen(after)
结果 先运行consumer.accept(t) ,接着运行 after.accept(t)
default Consumer<T> andThen(Consumer<? super T> after);
}
对应的测试结果:
Consumer<String> consumer= s-> System.out.println(s+"1");
consumer.accept("1");//11
consumer.andThen(s-> System.out.println(s+"2")).accept("1");//输出11 换行12
@FunctionalInterface
public interface Function<T, R> {
//输入t,返回r
R apply(T t);
//创建一个function实例, r= function.compose(before)
的结果 t = before.apply(v); r=function.apply(t)
default <V> Function<V, R> compose(Function<? super V, ? extends T> before);
//创建一个function实例, r=function.andThen(after)
的结果 v = function.apply(t); r=after.apply(v)
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after);
//t = Function.identity().apply(t)
static <T> Function<T, T> identity() {
return t -> t;
}
}
对应的测试结果:
Function<String,String> function = s-> s+"1";
System.out.println(function.apply("1"));//11
System.out.println(function.andThen(s -> s + "2").apply("1"));//112
System.out.println(function.compose(s -> s + "2").apply("1"));//121
System.out.println(Function.identity().apply("1"));//1
@FunctionalInterface
public interface Supplier<T> {
//返回T类型
T get();
}
对应的测试结果:
Supplier<String> supplier = ()->"1";
System.out.println(supplier.get());//1
3.方法引用
3.1 类名::静态方法名
3.2 instance::instance方法名
3.3 类名::instance方法名
3.4 类名::new
[1]: Java8 实战 java 8 in Action Mario Fusco 著 Alan Mycroft 陆明刚 劳佳 译