方法的引用-java8新特性
来源:互联网 发布:linux怎么改文件夹名 编辑:程序博客网 时间:2024/06/06 03:10
1
2
3
4
5
6
7
8
9
10
11
public
class
Person {
String name;
LocalDate birthday;
public
Calendar getBirthday() {
return
birthday;
}
public
static
int
compareByAge(Person a, Person b) {
return
a.birthday.compareTo(b.birthday);
}
}
1
Person[] rosterAsArray = roster.toArray(
new
Person[roster.size()]);
1
2
3
4
5
6
class
PersonAgeComparator
implements
Comparator<Person> {
public
int
compare(Person a, Person b) {
return
a.getBirthday().compareTo(b.getBirthday());
}
}
Arrays.sort(rosterAsArray,
new
PersonAgeComparator());
1
2
3
4
5
Arrays.sort(rosterAsArray,
(Person a, Person b) -> {
return
a.getBirthday().compareTo(b.getBirthday());
}
);
1
2
3
Arrays.sort(rosterAsArray,
(a, b) -> Person.compareByAge(a, b)
);
1
Arrays.sort(rosterAsArray, Person::compareByAge);
1
2
3
4
5
6
7
8
9
10
11
12
@FunctionalInterface
public
interface
IntBinaryOperator {
/**
* Applies this operator to the given operands.
*
* @param left the first operand
* @param right the second operand
* @return the operator result
*/
int
applyAsInt(
int
left,
int
right);
}
1
2
Example ex =
new
Example();
ex.oper(Example::mul,
1
,
2
);
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
publicclassExample {
publicintadd(inta, intb) {
returna + b;
}
publicstaticintmul(inta, intb) {
returna * b;
}
publicString lower(String a) {
returna.toLowerCase();
}
publicvoidprintDate(Date date) {
System.out.println(date);
}
publicvoidoper(IntBinaryOperator operator, inta, intb) {
System.out.println(operator.applyAsInt(a, b));
}
publicvoidoperS(Function<String, String> stringOperator, String a) {
System.out.println(stringOperator.apply(a));
}
publicGregorianCalendar operC(Supplier<GregorianCalendar> supplier) {
returnsupplier.get();
}
}
其实,在方法调用的过程中,已经传送了一个String的对象。引用构造方法(Reference to a constructor)ex.operC(()-> {return new GregorianCalendar();})ex.oper(GregorianCalendar::new)这个很简单,其实就是构造方法的简写。
0 0
- 方法的引用-java8新特性
- Java8新特性--方法引用
- Java8 新特性之三---------方法引用
- JAVA8新特性——方法引用
- JAVA8新特性——方法引用
- Java8新特性之三方法引用
- Java8新特性方法引用、构造器引用、数组引用
- java8新特性(方法引用、构造器引用)
- Java8新特性——方法和构造函数引用
- Android 使用Java8新特性之"方法引用"
- JAVA8新特性(三)——方法引用
- JAVA8的新特性
- Java8的新特性
- JAVA8的新特性
- java8的新特性
- Java8的新特性
- JAVA8的新特性
- Java8的新特性
- C# .NET 微信企业号通讯录添加成员
- 56 - Merge Intervals
- CodeForces 635A-Orchestra【图形规划】
- the resources of meteor
- sql 链接查询
- 方法的引用-java8新特性
- 引用和指针区别
- 线性回归
- RadioGroup 完成功能菜单效果
- static用法小结
- 12个强大的Web服务测试工具
- Leet Code OJ 223. Rectangle Area [Difficulty: Easy]
- 信息安全
- IOS图文混排