方法引用(Method References)
2024-10-20 19:52:45
* 方法引用的使用
*
* 1.使用情境:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
*
* 2.方法引用,本质上就是Lambda表达式,而Lambda表达式作为函数式接口的实例。所以
* 方法引用,也是函数式接口的实例。
*
* 3. 使用格式: 类(或对象) :: 方法名
*
* 4. 具体分为如下的三种情况:
* 情况1 对象 :: 非静态方法
* 情况2 类 :: 静态方法
*
* 情况3 类 :: 非静态方法
*
* 5. 方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的
* 形参列表和返回值类型相同!(针对于情况1和情况2)
// 情况一:对象 :: 实例方法
//Consumer中的void accept(T t)
//PrintStream中的void println(T t)
@Test
public void test1() {
Consumer<String> con1 = str -> System.out.println(str);
con1.accept("北京"); System.out.println("*******************");
PrintStream ps = System.out;
Consumer<String> con2 = ps::println;
con2.accept("beijing");
} //Supplier中的T get()
//Employee中的String getName()
@Test
public void test2() {
Employee emp = new Employee(1001,"Tom",23,5600); Supplier<String> sup1 = () -> emp.getName();
System.out.println(sup1.get()); System.out.println("*******************");
Supplier<String> sup2 = emp::getName;
System.out.println(sup2.get()); } // 情况二:类 :: 静态方法
//Comparator中的int compare(T t1,T t2)
//Integer中的int compare(T t1,T t2)
@Test
public void test3() {
Comparator<Integer> com1 = (t1,t2) -> Integer.compare(t1,t2);
System.out.println(com1.compare(12,21)); System.out.println("*******************"); Comparator<Integer> com2 = Integer::compare;
System.out.println(com2.compare(12,3)); } //Function中的R apply(T t)
//Math中的Long round(Double d)
@Test
public void test4() {
Function<Double,Long> func = new Function<Double, Long>() {
@Override
public Long apply(Double d) {
return Math.round(d);
}
}; System.out.println("*******************"); Function<Double,Long> func1 = d -> Math.round(d);
System.out.println(func1.apply(12.3)); System.out.println("*******************"); Function<Double,Long> func2 = Math::round;
System.out.println(func2.apply(12.6));
} // 情况三:类 :: 实例方法 (有难度)
// Comparator中的int comapre(T t1,T t2)
// String中的int t1.compareTo(t2)
@Test
public void test5() {
Comparator<String> com1 = (s1,s2) -> s1.compareTo(s2);
System.out.println(com1.compare("abc","abd")); System.out.println("*******************"); Comparator<String> com2 = String :: compareTo; //String 是因为调用参数为String
System.out.println(com2.compare("abd","abm"));
} //BiPredicate中的boolean test(T t1, T t2);
//String中的boolean t1.equals(t2)
@Test
public void test6() {
BiPredicate<String,String> pre1 = (s1,s2) -> s1.equals(s2);
System.out.println(pre1.test("abc","abc")); System.out.println("*******************");
BiPredicate<String,String> pre2 = String :: equals;
System.out.println(pre2.test("abc","abd"));
} // Function中的R apply(T t)
// Employee中的String getName();
@Test
public void test7() {
Employee employee = new Employee(1001, "Jerry", 23, 6000); Function<Employee,String> func1 = e -> e.getName();
System.out.println(func1.apply(employee)); System.out.println("*******************"); Function<Employee,String> func2 = Employee::getName;
System.out.println(func2.apply(employee)); }
最新文章
- wordpress默认index主页选择Your Projects提示无法找到项目解决办法?
- jquery 使用方法
- SharePoint 2013 中的 PowerPoint Automation Services
- loadrunner协议选择
- 实时数据显示--SignalR实例演示
- Delphi Excel 操作大全
- 如何加载JS
- jQuery 判断表单中多个 input text 中至少有一个不为空
- 权威第三方报告——获取IT产品竞争力信息的主要途径,类似你买电脑前上的xx论坛看实力评估
- 搭建SSH框架所需Jar包及其解释
- Sublime Text—设置浏览器快捷键
- 并行计算vs分布式计算
- 数据结构——Currency System in Geraldion
- 在MVC应用程序中动态加载PartialView
- 用枚举来处理java自定义异常
- Ubuntu14.04和Windows双系统时无法挂载磁盘解决方法
- IOC 和DI(转载)
- JQuery的事件委托;jQuery注册事件;jQuery事件解绑
- 第一行代码 3-5 软件也要拼脸蛋-UI界面-更强大的滚动条- 聊天室
- 使用shell删除目录下几天前生成文件方法