jdk提供我们的常用函数式接口:
Supplier;
Consumner;
Predicate;
Function;
02_函数式接口的概念&函数式接口的定义
概念: 有且仅有一个抽象方法的接口;
@FunctionalInterface
告诉编译器,我当前这个接口是函数式接口;(编译器就会检测当前接口是不是有且仅有一个抽象方法)
03_函数式接口的使用
lambda表达式的使用场景:
只有一个:
来替换函数式接口的实现类对象;
性能浪费的日志案例:
public static void showLog(int level,String message){
if(level==1){
System.out.println(message);
}
}
show(2,"1"+"2"+"3");
以上代码为啥会浪费性能;
不管有没有输出,肯定会做拼接;
05_使用Lambda优化日志案例
为什么使用Lambda表达式可以提高性能?
延迟特性:
原理:lambda表达式本质上是一个方法;只有显示的调用才会执行里面的代码;
06_函数式接口作为方法的参数案例
07_函数式接口作为方法的返回值类型案例
08_常用的函数式接口_Supplier接口
interface Supplier<T>{
T get();
}
使用的时候,用什么类型来替换泛型,这个接口就会生产什么类型的数据;
Supplier接口的抽象方法 get()是用来干什么的?
生产指定类型的数据;
09_常用的函数式接口_Supplier接口练习_求数组元素最大值
10_常用的函数式接口_Consumer接口
interface Consumer<T>{
accept(T t)
}
使用Consumer接口时,拿什么类型替换T,消费什么类型的数据
Comsumer接口的accept(T t)方法是用来干什么的?
就是用来消费数据的;
11_常用的函数式接口_Consumer接口的默认方法andThen
Comsumer接口的 Comsumer andThen(Comsumer c)这个方法做了什么?
public defualt Comsumer andThen(Comsumer c){
//con1.andThen(con2)
return (t)-{this.accept(t);c.accpt(t);};
}
Consumer con3 = con1.addThen(con2);
con3.accept("飞哥");
con1.addThen(con2).accept("飞哥");
13_常用的函数式接口_Predicate接口
interface Predicate<T>{
boolean test(T t);
}
使用的时候用什么类型替换T,那么该接口就判断什么类型的数据
Predicate接口的 boolean test(T t)这个方法的作用?
判断t这个数据是否满足一定的规则;
14_常用的函数式接口_Predicate接口_默认方法and
Predicate接口的 Predicate and(Predicate p)这个方法的作用?
public default Predicate and(Predicate p){
return (t)->{return this.test(t)&&p.test(t)};
}
15_常用的函数式接口_Predicate接口_默认方法or&negate
Predicate接口的 Predicate or(Predicate p)这个方法的作用?
Predicate接口的 Predicate negate()这个方法的作用?
public default Predicate or(Predicate p){
return (t)->{return this.test(t)||p.test(t)};
}
public default Predicate negate(){
return (t)->{return !this.test(t)};
}
17_常用的函数式接口_Function接口
interface Function<T,R>{
R apply(T t);
}
把T类型的数据转换成 R类型的数据;
Function接口的 R apply(T t)方法有什么作用?
18_常用的函数式接口_Function接口_默认方法andThen
Function接口的 Function<T,V> andThen(Function<R,V> f)的作用?
19_常用的函数式接口_Function接口练习_自定义函数模型拼接
1.String--->String;
2.String--->Integer;
3.Integer--->Integer;
/*
List<String> list = new ArrayList<>();
list.add("柳岩,女,18");
list.add("凤姐,男,19");
list.add("芙蓉,女,20");
list.add("鹿晗,男,21");
list.add("关晓彤,女,22");
list.add("李若彤,女,44");
list.add("李雪健,男,20");
list.add("胡歌,男,21");
需求:
1.使用前7个元素;
2.跳过第1个元素;
3.只保留女的;
4.只保留年龄小于30岁的;
5.把每条数据映射成一个User对象;Function<String,User>
6.遍历结果;
public class Test{
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("柳岩,女,18");
list.add("凤姐,男,19");
list.add("姐姐,女,20");
list.add("鹿晗,男,21");
list.add("关晓彤,女,22");
list.add("李若彤,女,44");
list.add("李雪健,男,20");
list.add("胡歌,男,21");
// 通过集合对象获取到对应的流对象;
/* Stream<String> stream = list.stream();
// 1.使用前7个元素;
Stream<String> stream2 = stream.limit(7);
// 2.跳过第1个元素;
Stream<String> stream3 = stream2.skip(1);
// 3.只保留女的;
Stream<String> stream4 = stream3.filter((str) -> {
String[] split = str.split(",");
String gender = split[1];
return "女".equals(gender);
});
// 4.只保留年龄小于30岁的;
Stream<String> stream5 = stream4.filter((str) -> {
String[] split = str.split(",");
int age = Integer.valueOf(split[2]);
return age<30;
});
// 5.把每条数据映射成一个User对象;Function<String,User>
Stream<User> stream6 = stream5.map((str) -> {
String[] split = str.split(",");
String name = split[0];
String gender = split[1];
int age = Integer.valueOf(split[2]);
User user = new User(name, age, gender);
return user;
});
// 6.遍历结果;
*//*stream6.forEach((user)->{
System.out.println(user);
});
*//*
List<User> result = stream6.collect(Collectors.toList());
for (User user : result) {
System.out.println(user);
}*/
list.stream()
.limit(7)//截取
.skip(1)//跳过
.filter(str -> {
String[] split = str.split(",");
String gender = split[1];
return "女".equals(gender);
})//过滤女的
.filter((str) -> {
String[] split = str.split(",");
int age = Integer.valueOf(split[2]);
return age<30;
})//过滤30岁以下的
.map((str) -> {
String[] split = str.split(",");
String name = split[0];
String gender = split[1];
int age = Integer.valueOf(split[2]);
User user = new User(name, age, gender);
return user;
})//映射成user对象
.forEach((user)->{
System.out.println(user);
});//输出
}
}