1.函数式接口1.1函数式接口概述【理解】
-
概念
有且仅有一个抽象方法的接口
-
如何检测一个接口是不是函数式接口
@FunctionalInterface
放在接口定义的上方:如果接口是函数式接口,编译通过;如果不是,编译失败
-
注意事项
我们自己定义函数式接口的时候,@FunctionalInterface是可选的,就算我不写这个注解,只要保证满足函数式接口定义的条件,也照样是函数式接口。但是,建议加上该注解
1.2函数式接口作为方法的参数【应用】
-
需求描述
定义一个类(RunnableDemo),在类中提供两个方法
一个方法是:startThread(Runnable r) 方法参数Runnable是一个函数式接口
一个方法是主方法,在主方法中调用startThread方法
-
代码演示
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
publicclassRunnableDemo{public static voidmain(String[] args){//在主方法中调用startThread方法//匿名内部类的方式startThread(newRunnable(){@Overridepublic voidrun(){System.out.println(Thread.currentThread().getName()+"线程启动了");}});//Lambda方式startThread(()->System.out.println(Thread.currentThread().getName()+"线程启动了"));}private static void startThread(Runnable r){newThread(r).start();}}
1.3函数式接口作为方法的返回值【应用】
-
需求描述
定义一个类(ComparatorDemo),在类中提供两个方法
一个方法是:Comparator<String> getComparator() 方法返回值Comparator是一个函数式接口
一个方法是主方法,在主方法中调用getComparator方法
-
代码演示
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
publicclassComparatorDemo{public static voidmain(String[] args){//定义集合,存储字符串元素ArrayList<String>array=newArrayList<String>();array.add("cccc");array.add("aa");array.add("b");array.add("ddd");System.out.println("排序前:"+array);Collections.sort(array,getComparator());System.out.println("排序后:"+array);}private static Comparator<String>getComparator(){//匿名内部类的方式实现//returnnewComparator<String>(){//@Override//public int compare(String s1,String s2){//returns1.length()-s2.length();//}//};//Lambda方式实现return(s1,s2)->s1.length()-s2.length();}}
1.8常用函数式接口之Predicate【应用】
-
Predicate接口
Predicate<T>接口通常用于判断参数是否满足指定的条件
-
常用方法
[td]方法名
说明
boolean test(T t)
对给定的参数进行判断(判断逻辑由Lambda表达式实现),返回一个布尔值
default Predicate<T> negate()
返回一个逻辑的否定,对应逻辑非
default Predicate<T> and(Predicate other)
返回一个组合判断,对应短路与
default Predicate<T> or(Predicate other)
返回一个组合判断,对应短路或
-
代码演示
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
publicclassPredicateDemo01{public static voidmain(String[] args){booleanb1=checkString("hello",s->s.length()>8);System.out.println(b1);booleanb2=checkString("helloworld",s->s.length()>8);System.out.println(b2);}//判断给定的字符串是否满足要求private staticbooleancheckString(String s,Predicate<String>pre){//return!pre.test(s);returnpre.negate().test(s);}}
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
publicclassPredicateDemo02{public static voidmain(String[] args){booleanb1=checkString("hello",s->s.length()>8);System.out.println(b1);booleanb2=checkString("helloworld",s->s.length()>8);System.out.println(b2);booleanb3=checkString("hello",s->s.length()>8,s->s.length()<15);System.out.println(b3);booleanb4=checkString("helloworld",s->s.length()>8,s->s.length()<15);System.out.println(b4);}//同一个字符串给出两个不同的判断条件,最后把这两个判断的结果做逻辑与运算的结果作为最终的结果private staticbooleancheckString(String s,Predicate<String>pre1,Predicate<String>pre2){returnpre1.or(pre2).test(s);}//判断给定的字符串是否满足要求private staticbooleancheckString(String s,Predicate<String>pre){returnpre.test(s);}}
- Strem流2.1体验Stream流【理解】
-
案例需求
按照下面的要求完成集合的创建和遍历
-
创建一个集合,存储多个字符串元素
-
把集合中所有以"张"开头的元素存储到一个新的集合
-
把"张"开头的集合中的长度为3的元素存储到一个新的集合
-
遍历上一步得到的集合
-
-
原始方式示例代码
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
publicclassStreamDemo{public static voidmain(String[] args){//创建一个集合,存储多个字符串元素ArrayList<String>list=newArrayList<String>();list.add("林青霞");list.add("张曼玉");list.add("王祖贤");list.add("柳岩");list.add("张敏");list.add("张无忌");//把集合中所有以"张"开头的元素存储到一个新的集合ArrayList<String>zhangList=newArrayList<String>();for(String s:list){if(s.startsWith("张")){zhangList.add(s);}}//System.out.println(zhangList);//把"张"开头的集合中的长度为3的元素存储到一个新的集合ArrayList<String>threeList=newArrayList<String>();for(String s:zhangList){if(s.length()==3){threeList.add(s);}}//System.out.println(threeList);//遍历上一步得到的集合for(String s:threeList){System.out.println(s);}System.out.println("--------");//Stream流来改进//list.stream().filter(s->s.startsWith("张")).filter(s->s.length()==3).forEach(s->System.out.println(s));list.stream().filter(s->s.startsWith("张")).filter(s->s.length()==3).forEach(System.out::println);}} -
使用Stream流示例代码
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
publicclassStreamDemo{public static voidmain(String[] args){//创建一个集合,存储多个字符串元素ArrayList<String>list=newArrayList<String>();list.add("林青霞");list.add("张曼玉");list.add("王祖贤");list.add("柳岩");list.add("张敏");list.add("张无忌");//Stream流来改进list.stream().filter(s->s.startsWith("张")).filter(s->s.length()==3).forEach(System.out::println);}} -
Stream流的好处
-
直接阅读代码的字面意思即可完美展示无关逻辑方式的语义:获取流、过滤姓张、过滤长度为3、逐一打印
-
Stream流把真正的函数式编程风格引入到Java中
-
-
1479

被折叠的 条评论
为什么被折叠?



