黑马程序员技术交流社区

标题: 【成都校区】关于函数式接口的一些笔记 [打印本页]

作者: 生死无关    时间: 2018-11-29 12:55
标题: 【成都校区】关于函数式接口的一些笔记
函数式接口:
    有且仅有一个抽象方法的接口就是函数式接口(Object中定义的方法除外、静态方法、默认方法都不关注)

    使用注解:@FunctionalInterface可以去检查一个接口是否是函数式接口

Lambda:
    使用:用于简化匿名内部类,它本身不是匿名内部类。

    组成:
        一些参数 () 和被重写的方法的参数保持一致
        一个箭头 ->
        一段代码 {} 重写的方法要执行什么代码

    lambda的简化格式:
        1. 在参数列表中参数的类型可以省略
        2. 如果参数只有一个,连参数外的小括号都可以省略
        3. 如果重写方法中的代码只有一行,可以省略大括号分号和return
            要么都省略要么都不省略

    lambda优点:
        1. 简化代码书写
        2. 相比于匿名内部类,lambda不会生成class文件,一定程度上节约资源
        3. 相比于普通的代码,lambda有延迟执行的效果,被调用的时候才执行

常用的函数式接口:
    1. Supplier<T> 生产型接口
        T get()
练习:
public class Demo02Test {
//定一个方法,方法的参数传递Supplier,泛型使用Integer
public static int getMax(Supplier<Integer> sup){
return sup.get();
}
public static void main(String[] args) {
int arr[] = {2,3,4,52,333,23};
//调用getMax方法,参数传递Lambda
int maxNum = getMax(()‐>{
//计算数组的最大值
int max = arr[0];
for(int i : arr){
if(i>max){
max = i;
}
}
return max;
});
System.out.println(maxNum);
}
}
    2. Consumer<T> 消费型接口
        void accept(T t)
        默认方法:
            andThen(Consumer after)  可以额外接收一个Consumer类型的数据,共同消费一个数据
练习:
public class DemoConsumer {
public static void main(String[] args) {
String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" };
printInfo(s ‐> System.out.print("姓名:" + s.split(",")[0]),
s ‐> System.out.println("。性别:" + s.split(",")[1] + "。"),
array);
}
private static void printInfo(Consumer<String> one, Consumer<String> two, String[] array) {
for (String info : array) {
one.andThen(two).accept(info); // 姓名:迪丽热巴。性别:女。
}
}
}
    3. Predicate<T> 判断型接口
        boolean test(T t)
        默认方法:
            and(Predicate other)  两个判断都需要满足结果才满足,相当于 &&
            or(Predicate other)  两个判断满足其一即可,相当于 ||
            negate()    取反 !
练习:
public class DemoPredicate {
public static void main(String[] args) {
String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女" };
List<String> list = filter(array,
s ‐> "女".equals(s.split(",")[1]),
s ‐> s.split(",")[0].length() == 4);
System.out.println(list);
}
private static List<String> filter(String[] array, Predicate<String> one,
Predicate<String> two) {
List<String> list = new ArrayList<>();
for (String info : array) {
if (one.and(two).test(info)) {
list.add(info);
}
}
return list;
}
}
    4. Function<T,R> 转换型接口
        R apply(T t)
        默认方法:
            andThen(Function after)
            compose(Function before)

            f1.andThen(f2).apply(t); f1先执行转换,得到一个结果作为第二次转换的参数
                等同于:f2.apply(f1.apply(t))


            f1.compose(f2).apply(t);
            f1.apply(f2.apply(t))
练习:
public class DemoFunction {
public static void main(String[] args) {
String str = "赵丽颖,20";
int age = getAgeNum(str, s ‐> s.split(",")[1],
s ‐>Integer.parseInt(s),
n ‐> n += 100);
System.out.println(age);
}
private static int getAgeNum(String str, Function<String, String> one,
Function<String, Integer> two,
Function<Integer, Integer> three) {
return one.andThen(two).andThen(three).apply(str);
}
}
        默认方法一般都是延迟方法
        抽象方法一般都是终结方法






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2