lambda 表达式
Lambda 表达式(lambda expression)是一个匿名函数,简化我们调用匿名函数的过程。
Lambda表达式的规范
使用Lambda表达式 依赖于函数接口
1.在接口中只能够允许有一个抽象方法
2.在函数接口中定义object类中方法
3.使用默认或者静态方法
4.@FunctionalInterface 表示该接口为函数接口
Java中使用Lambda表达式的规范,必须是为函数接口
函数接口的定义:在该接口中只能存在一个抽象方法,该接口称作为函数接口
Java中的Lambda表达式的规范,必须是为函数接口。
函数接口的定义:在该接口中只能存在一个抽象方法,该接口称作为函数接口
JDK中自带的函数接口:
java.lang.Runnable
java.util.concurrent.Callable
java.security.PrivilegedAction
java.util.Comparator
java.io.FileFilter
java.nio.file.PathMatcher
java.lang.reflect.InvocationHandler
java.beans.PropertyChangeListener
java.awt.event.ActionListener
javax.swing.event.ChangeListener
我们也可以使用@FunctionalInterface修饰为函数接口
函数接口定义
1.在接口中只能有一个抽象方法
2.@FunctionalInterface 标记为该接口为函数接口
3.可以通过default 修饰为普通方法
4.可以定义object类中的方法
@FunctionalInterface
public interface MyFunctionalInterface {
void add();
default void get() {
}
String toString();
}
Lambda基础语法
() 参数列表
-> 分隔
{} 方法体
(a,b)->{
}
无参方法调用
带参数方法调用
(函数接口的参数列表 不需要写类型 需要定义参数名称)->{方法体}
二、函数式接口
JUF 中内置的函数式接口
Java系统内置那些函数接口
bash
消费型接口:
Conusmer<T>
void accept(T t);
BiConusmer<T,U>
void accept(T t,U u);//增加一种入参类型
供给型接口
Supplier<T>
void get();
函数型接口
Function<T ,R>
R apply(T t);
UnaryOperator<T>
T apply(T t);//入参与返回值类型一致
BiFunction <T ,U,R>
R apply(T t,U u);//增加一个参数类型
BinaryOperator<T>
T apply(T t1,T t2);//l两个相同类型入参与同类型返回值
ToIntFunction<T>//限定返回int
ToLongFunction<T>//限定返回long
ToDoubleFunction<T>//限定返回double
IntFunction<R>//限定入参int,返回泛型R
LongFunction<R>//限定入参long,返回泛型R
DoubleFunction<R>//限定入参double,返回泛型R
断言型接口
Predicate<T>
boolean test(T t);
常用 函数接口用法示例
/**
* java8 JUF 工具包中提供的工具
* 1元函数接口
* Function,Consumer,Supplier,Predicate
* 2元函数接口
* BiFunction,BiConsumer,BiPredicate,BinaryOperator
* 函数式接口传入的是lambda表达式,lambda中的抽象方法只有一个。因此定义的函数的是这个抽象方法;
* lambda 中的静态方法和抽象方法都可以有,一般返回的是函数本身;
* 函数使用的两个步骤,1.定义函数的抽象方法的逻辑,二调用函数执行
*
* @param args
*/
一元函数
Function
/**
* 一元函数 function
*/
public static void functionDemo(){
/**
* R apply(T var1);
* 传入一个参数T返回一个结果R
*/
Function<Integer,Integer> function1 = i-> i+2;
System.out.println(function1.apply(10));
/**
*
* 组合函数compose:先运行的传入的参数1,后运行外层的方法2.
* 传入一个参数,返回一个结果Function<V, R>
* default <V> Function<V, R> compose(Function<? super V, ? extends T> var1)
* return this.apply(var1.apply(var2));
*
* 先运行传入的参数function1(var1) 的运行结果,作为参数传入function2
*/
Function<Integer,Integer> function2= i-> i*2;
Function<Integer,Integer> function3= function2.compose(function1);
System.out.println(function3.apply(5));
/**
* 组合函数andThen:先运行函数function2 ,后运行函数function1,
** 传入一个参数,返回一个结果Function<T, V>
* 先运行传入的参数function2(var1) 的运行结果(因为R apply(T var1)返回结果R),作为参数传入function1
*
*/
Function<Integer,Integer> function4= function2.andThen(function1);
System.out.println(function4.apply(5));
}
返回结果
12
14
12
ConSumer
/**
* 消费者ConSumer 函数:传入一个参数,运行一段逻辑,没有返回结果
*/
public static void consumer(){
/**
* consumer函数:接受一个参数,运行一段逻辑
* 返回的结果是空: void accept(T var1);
*/
Consumer<String> consumer1 = str1-> System.out.println(str1.toUpperCase());
consumer1.accept("hello ");
Consumer<String> consumer2 = str2-> {
String str3=String.format("%s Word",str2) ;
System.out.println(str3);
};
/**
* andThen:方法组合,先运行方法1consumer1(没有返回结果),后运行方法consumer2;
* 返回的结果是Consumer<T>
*/
Consumer<String> consumer3= consumer1.andThen(consumer2);
consumer3.accept("hello1");
}
返回结果
HELLO
HELLO1
hello1 Word
Supplier
/**
* supplier 提供者
* T get();
*
*/
public static void supplier(){
Supplier<Integer> s = ()-> {
System.out.println(Integer.valueOf(10000));
return 10000;
};
s.get();
}
返回结果
10000
Predicate
/**
* 断言
* Predicate
* @param args
*/
public static void predicate(){
Predicate<String> p1 = (String s)-> s.equals("pre");
/**
* test:boolean test(T var1);
* 输入一个参数,执行一段逻辑,返回一个结果
*
*/
System.out.println(p1.test("pre"));
Predicate<String> p2 = s-> s.length()>=3?false:true;
/**
* 两个Predicate类型函数逻辑且运算
* 输入参数:
* default Predicate<T> and(Predicate<? super T> var1)
* 返回结果:Predicate<T> 类型
*
*/
Predicate p3= p2.and(p1);
System.out.println(p3.test("pre"));
/**
* 两个Predicate类型函数逻辑与运算
* 输入参数:Predicate 类型
* default Predicate<T> and(Predicate<? super T> var1)
* 返回结果:Predicate<T> 类型
*/
Predicate p4= p2.or(p1);
System.out.println(p4.test("pre"));
/**
* 两个Predicate类型函数逻辑与运算
* 输入参数:Predicate 类型
* default Predicate<T> negate()
* 返回结果:Predicate<T> 类型
*/
System.out.println(p1.test("pre"));
System.out.println(p1.negate().test("pre"));
}
返回结果
true
false
true
true
false
comparator
public static void comparator() {
Comparator<Integer> comparator1 = (Integer s1, Integer s2) -> s1 > s2 ? 1 : -1;
System.out.println("comparator1.compare:" + comparator1.compare(13, 15));
System.out.println("comparator1.equals:" + comparator1.equals(comparator1));
}
返回结果
comparator1.compare:-1
comparator1.equals:true
二元函数
biFunction
/**
* BiFunction 二元函数
*/
public static void biFunction(){
BiFunction<Integer,Integer,String> biFunction1= (s1,s2)-> String.valueOf(s1+s2).concat(" good!");
/**
* 出入两个参数,返回一个结果
*/
System.out.println(biFunction1.apply(10,15));
BiFunction<Integer,Integer,String> biFunction2= (s1,s2)-> String.valueOf(s1*s2).concat(" very good!");
Function<String,String> function3 = str-> str.toUpperCase().concat("!!!");
/**
* 先运行biFunction2,后运行function3
* default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> var1)
* 入参:一元Function 类型函数
* 返回结果:二元BiFunction 类型函数
*/
BiFunction<Integer,Integer,String> biFunction3=biFunction2.andThen(function3);
System.out.println(biFunction3.apply(2,3));
}
返回结果
25 good!
6 VERY GOOD!!!
BiConsumer
/**
* 二元函数
* 消费者 BiConsumer
* @param args
*/
public static void biConsumer(){
BiConsumer<Integer,Integer> biConsumer1 = (s1,s2)-> System.out.println((s1-s2));
biConsumer1.accept(13,14);
/**
*andThen:先执行
*default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> var1)
* 入参:BiConsumer<? super T, ? super U> 类型。参数的类型要和biConsumer2的保持一致
* 返回结果:BiConsumer<T, U>
*/
BiConsumer<Integer,Integer> biConsumer2 = (s1,s2)-> System.out.println((s1+s2));
biConsumer2.andThen(biConsumer1).accept(12,14);
}
返回结果
-1
26
-2
BiPredicate
public static void BiPredicate(){
BiPredicate<Integer,Integer> biPredicate1 =(s1,s2)->s1>s2;
System.out.println(biPredicate1.test(13,15));
/**
* negate 逻辑取反
* default BiPredicate<T, U> negate()
*入参 :无
* 出参:BiPredicate<T, U>,
* 获取出参的返回结果:test(13,15)
*/
System.out.println(biPredicate1.negate().test(13,15));
BiPredicate<Integer,Integer> biPredicate2 =(s1,s2)->s1>7&&s2<12;
BiPredicate<Integer,Integer> biPredicate3 =(s3,s4)->8>s3&&s4<10;
/**
* negate 逻辑取反
* default BiPredicate<T, U> negate()
*入参 :BiPredicate<? super T, ? super U> var1
* 出参:BiPredicate<T, U>, 返回的结果biPredicate3与biPredicate1 结果取与 运算
* 获取出参的返回结果:test(9,11)
*/
System.out.println(biPredicate3.or(biPredicate2).test(9,11));
}
返回结果
false
true
true