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
public
class
RunnableDemo
{
public static void
main
(
String[] args
)
{
/
/
在主方法中调用startThread方法
/
/
匿名内部类的方式
startThread
(
new
Runnable
(
)
{
@Override
public void
run
(
)
{
System.out.println
(
Thread.currentThread
(
)
.getName
(
)
+
"线程启动了"
)
;
}
}
)
;
/
/
Lambda方式
startThread
(
(
)
-
>
System.out.println
(
Thread.currentThread
(
)
.getName
(
)
+
"线程启动了"
)
)
;
}
private static void startThread
(
Runnable r
)
{
new
Thread
(
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
public
class
ComparatorDemo
{
public static void
main
(
String[] args
)
{
/
/
定义集合,存储字符串元素
ArrayList
<
String
>
array
=
new
ArrayList
<
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
(
)
{
/
/
匿名内部类的方式实现
/
/
return
new
Comparator
<
String
>
(
)
{
/
/
@Override
/
/
public int compare
(
String s
1
,
String s
2
)
{
/
/
return
s
1.
length
(
)
-
s
2.
length
(
)
;
/
/
}
/
/
}
;
/
/
Lambda方式实现
return
(
s
1
,
s
2
)
-
>
s
1.
length
(
)
-
s
2.
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
public
class
PredicateDemo
01
{
public static void
main
(
String[] args
)
{
boolean
b
1
=
checkString
(
"hello"
,
s
-
>
s.length
(
)
>
8
)
;
System.out.println
(
b
1
)
;
boolean
b
2
=
checkString
(
"helloworld"
,
s
-
>
s.length
(
)
>
8
)
;
System.out.println
(
b
2
)
;
}
/
/
判断给定的字符串是否满足要求
private static
boolean
checkString
(
String s
,
Predicate
<
String
>
pre
)
{
/
/
return
!pre.test
(
s
)
;
return
pre.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
public
class
PredicateDemo
02
{
public static void
main
(
String[] args
)
{
boolean
b
1
=
checkString
(
"hello"
,
s
-
>
s.length
(
)
>
8
)
;
System.out.println
(
b
1
)
;
boolean
b
2
=
checkString
(
"helloworld"
,
s
-
>
s.length
(
)
>
8
)
;
System.out.println
(
b
2
)
;
boolean
b
3
=
checkString
(
"hello"
,
s
-
>
s.length
(
)
>
8
,
s
-
>
s.length
(
)
<
15
)
;
System.out.println
(
b
3
)
;
boolean
b
4
=
checkString
(
"helloworld"
,
s
-
>
s.length
(
)
>
8
,
s
-
>
s.length
(
)
<
15
)
;
System.out.println
(
b
4
)
;
}
/
/
同一个字符串给出两个不同的判断条件,最后把这两个判断的结果做逻辑与运算的结果作为最终的结果
private static
boolean
checkString
(
String s
,
Predicate
<
String
>
pre
1
,
Predicate
<
String
>
pre
2
)
{
return
pre
1.
or
(
pre
2
)
.test
(
s
)
;
}
/
/
判断给定的字符串是否满足要求
private static
boolean
checkString
(
String s
,
Predicate
<
String
>
pre
)
{
return
pre.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
public
class
StreamDemo
{
public static void
main
(
String[] args
)
{
/
/
创建一个集合,存储多个字符串元素
ArrayList
<
String
>
list
=
new
ArrayList
<
String
>
(
)
;
list
.
add
(
"林青霞"
)
;
list
.
add
(
"张曼玉"
)
;
list
.
add
(
"王祖贤"
)
;
list
.
add
(
"柳岩"
)
;
list
.
add
(
"张敏"
)
;
list
.
add
(
"张无忌"
)
;
/
/
把集合中所有以
"张"
开头的元素存储到一个新的集合
ArrayList
<
String
>
zhangList
=
new
ArrayList
<
String
>
(
)
;
for
(
String s
:
list
)
{
if
(
s.startsWith
(
"张"
)
)
{
zhangList.
add
(
s
)
;
}
}
/
/
System.out.println
(
zhangList
)
;
/
/
把
"张"
开头的集合中的长度为
3
的元素存储到一个新的集合
ArrayList
<
String
>
threeList
=
new
ArrayList
<
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
public
class
StreamDemo
{
public static void
main
(
String[] args
)
{
/
/
创建一个集合,存储多个字符串元素
ArrayList
<
String
>
list
=
new
ArrayList
<
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中
-
-