前言
之前两篇文章分别介绍了Java8的lambda表达式和默认方法和静态接口方法。今天我们继续学习Java8的新语言特性——方法引用(Method References)。
在学习lambda表达式之后,我们通常使用lambda表达式来创建匿名方法。然而,有时候我们仅仅是调用了一个已存在的方法。如下:
?
1
|
Arrays.sort(stringsArray,(s1,s2)->s1.compareToIgnoreCase(s2));
|
在Java8中,我们可以直接通过方法引用来简写lambda表达式中已经存在的方法。
?
1
|
Arrays.sort(stringsArray, String::compareToIgnoreCase);
|
这种特性就叫做方法引用(Method Reference)。
方法引用的标准形式是: 类名::方法名 。(注意:只需要写方法名,不需要写括号)
类型 | 示例 |
---|---|
引用静态方法 | ContainingClass::staticMethodName |
引用某个对象的实例方法 | containingObject::instanceMethodName |
引用某个类型的任意对象的实例方法 | ContainingType::methodName |
引用构造方法 | ClassName::new |
?
1
2
3
4
5
6
7
8
9
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
46
47
48
49
50
51
52
53
54
|
public class Person {
public enum Sex{
MALE,FEMALE
}
String name;
LocalDate birthday;
Sex gender;
String emailAddress;
public String getEmailAddress() {
return emailAddress;
}
public Sex getGender() {
return gender;
}
public LocalDate getBirthday() {
return birthday;
}
public String getName() {
return name;
}
public static int compareByAge(Person a,Person b){
return a.birthday.compareTo(b.birthday);
}
}
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Person [] persons = new Person[ 10 ];
//使用匿名类
Arrays.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.birthday.compareTo(o2.birthday);
}
});
//使用lambda表达式
Arrays.sort(persons, (o1, o2) -> o1.birthday.compareTo(o2.birthday));
//使用lambda表达式和类的静态方法
Arrays.sort(persons, (o1, o2) -> Person.compareByAge(o1,o2));
//使用方法引用
//引用的是类的静态方法
Arrays.sort(persons, Person::compareByAge);
|
?
1
2
3
4
5
6
7
8
9
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
|
class ComparisonProvider{
public int compareByName(Person a,Person b){
return a.getName().compareTo(b.getName());
}
public int compareByAge(Person a,Person b){
return a.getBirthday().compareTo(b.getBirthday());
}
}
ComparisonProvider provider = new ComparisonProvider();
//使用lambda表达式
//对象的实例方法
Arrays.sort(persons,(a,b)->provider.compareByAge(a,b));
//使用方法引用
//引用的是对象的实例方法
Arrays.sort(persons, provider::compareByAge);
|
?
1
2
3
4
5
6
7
8
9
10
11
|
String[] stringsArray = { "Hello" , "World" };
//使用lambda表达式和类型对象的实例方法
Arrays.sort(stringsArray,(s1,s2)->s1.compareToIgnoreCase(s2));
//使用方法引用
//引用的是类型对象的实例方法
Arrays.sort(stringsArray, String::compareToIgnoreCase);
|
?
1
2
3
4
5
6
7
8
9
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
|
public static <T, SOURCE extends Collection<T>, DEST extends Collection<T>>
DEST transferElements(SOURCE sourceColletions, Supplier<DEST> colltionFactory) {
DEST result = colltionFactory.get();
for (T t : sourceColletions) {
result.add(t);
}
return result;
}
...
final List<Person> personList = Arrays.asList(persons);
//使用lambda表达式
Set<Person> personSet = transferElements(personList,()-> new HashSet<>());
//使用方法引用
//引用的是构造方法
Set<Person> personSet2 = transferElements(personList, HashSet:: new );
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/JohnTsai/p/5806194.html
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 38
-
2025-05-29 88
-
2025-05-25 19
-
2025-05-27 84
-
2025-05-29 43
热门评论