lambda表达式语法说明
2019-04-03 本文已影响0人
虫儿飞ZLEI
1.基础语法
1.1 "->" 操作符(lambda操作符)
"->" 操作符将整个lambda表达式拆分成两部分
左侧:表达式的参数列表(接口中方法的参数列表)
右侧:接口中方法的实现
ps:如果接口中有多个方法,就不好使了
1.2 demo
1.2.1 无返回值,无参数
()->xxxx;
Runnable r1 = new Runnable(){
@Override
public void run() {
System.out.println("r1");
}
};
Runnable r2 = ()-> System.out.println("r2");
1.2.2 无返回值,一个参数
(x) -> System.out.println(x);
//x是形参
Consumer<String> consumer = (x) -> System.out.println(x);
consumer.accept("xxxxxxxxxxx");
还能简写成:
Consumer<String> consumer = System.out::println;
consumer.accept("xxxxxxxxxxx");
1.2.3 有返回值,多个参数,lambda体中有多条语句
(x,y)->{
System.out.println("xxx");
return Integer.compare(x,y);
};
Comparator<Integer> com = (x,y)->{
System.out.println("xxx");
return Integer.compare(x,y);
};
int i = com.compare(2, 1);
System.out.println(i);
如果只有一条语句,return和{}都可以省略不写
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
int i = com.compare(2, 1);
System.out.println(i);
2. lambda表达式需要函数式接口的支持
只有一个抽象方法的接口就叫函数式接口
可以使用@FunctionalInterface修饰接口,此时写多个抽象方法会报错
3. java8 内置四大核心函数式接口
3.1 Consumer
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
3.2 Supplier
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
3.3 Function
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
3.4 Predicate
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
3.5 尝试
public class Main {
public static void main(String[] args) {
happy(1.0,(m)->{
System.out.println(m+1);
});
}
private static void happy(double money,Consumer<Double> consumer){
consumer.accept(money);
}
}
4. 方法引用
若lambda体中的内容有方法已经实现了,就可以使用方法引用(意思是lambda体中就调用了一个方法,没有别的语句)
这个方法的参数和返回值需要和接口中的方法的参数和返回值一致。
三种语法格式:
- 对象::实例方法名
- 类::静态方法名
- 类::实例方法名
Consumer<String> con = (x)-> System.out.println(x);
//等价于
Consumer<String> con2 = System.out::println;
Comparator<Integer> comparator = (x,y)->Integer.compare(x,y);
//等价于
Comparator<Integer> comparator2 = Integer::compare;
BiPredicate<String,String> biPredicate = (x,y)->x.equals(y);
//等价于
//调用时传进来的两个参数,需要满足第一个参数x是String类型,第二个参数是equals的参数类型
BiPredicate<String,String> biPredicate2 = String::equals;
5. 构造器引用
格式:
classname::new
Supplier<Person> p = ()->new Person();
//等价于
Supplier<Person> p2 = ()->Person::new;