博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java8中方法引用
阅读量:3965 次
发布时间:2019-05-24

本文共 3176 字,大约阅读时间需要 10 分钟。

1 理解:

方法引用,本质上就是Lambda表达式,而Lambda表达式作为函数式接口的实例。所以方法引用,也是函数式接口的实例。

在这里插入图片描述

2 使用情境:

当要传递给Lambda体的操作,已经实现的方法了,可以使用方法引用!

3 要求:

在这里插入图片描述

4 格式:

类(或对象) :: 方法名

5 分为如下的三种情况:

  • 情况1 对象 :: 非静态方法
  • 情况2 类 :: 静态方法
  • 情况3 类 :: 非静态方法
    在这里插入图片描述

6.说明:

// 情况一:对象 :: 实例方法

//Consumer中的void accept(T t)	//PrintStream中的void println(T t)	@Test	public void test1() {
Consumer
com1 = s -> System.out.println(s); com1.accept("朴灿烈"); System.out.println("*******************");// PrintStream ps = System.out;// Consumer
con2 = ps::println; Consumer
com2 = System.out :: println; com2.accept("张艺兴"); } //Supplier中的T get() //Employee中的String getName() @Test public void test2() {
Employee e = new Employee(1001,"朴灿烈",29,100000); //普通方法 Supplier
sup1 = new Supplier
() {
@Override public String get() {
return e.getName(); } }; System.out.println(sup1.get()); System.out.println("*********************"); //Lambda表达式 Supplier
sup2 = () -> e.getName(); System.out.println(sup2.get()); System.out.println("*********************"); //方法引用 Supplier
sup3 = e :: getName; System.out.println(sup3.get()); }

情况二:类 :: 静态方法

//Comparator中的int compare(T t1,T t2)	//Integer中的int compare(T t1,T t2)	@Test	public void test3() {
Comparator
com1 = (o1,o2) -> Integer.compare(o1,o2); System.out.println(com1.compare(12,21)); System.out.println("****************"); Comparator
com2 = Integer::compare; System.out.println(com2.compare(12,21)); } //Function中的R apply(T t) //Math中的Long round(Double d) @Test public void test4() {
Function
fun = new Function
() {
@Override public Long apply(Double d) {
return Math.round(d); } }; System.out.println(fun.apply(2.0)); System.out.println("****************"); Function
fun1 = t -> Math.round(t); System.out.println(fun1.apply(2.0)); System.out.println("****************"); Function
fun2 = Math :: round; System.out.println(fun2.apply(2.0)); System.out.println("****************"); }

// 情况:类 :: 实例方法

// Comparator中的int comapre(T t1,T t2)	// String中的int t1.compareTo(t2)	@Test	public void test5() {
Comparator
com = (s1,s2) -> s1.compareTo(s2); System.out.println(com.compare("abc","abd")); System.out.println("****************"); Comparator
com1 = String :: compareTo; System.out.println(com1.compare("abc","abd")); } //BiPredicate中的boolean test(T t1, T t2); //String中的boolean t1.equals(t2) @Test public void test6() {
BiPredicate
bip1 = (s1,s2) -> s1.equals(s2); System.out.println(bip1.test("abc", "abc")); System.out.println("**************"); BiPredicate
bip2 = String ::equals; System.out.println(bip1.test("abc", "abc")); } // Function中的R apply(T t) // Employee中的String getName(); @Test public void test7() {
Employee e = new Employee(1002,"朴灿烈",29,10000); Function
fun1 = emp -> emp.getName(); System.out.println(fun1.apply(e)); System.out.println("*************************"); Function
fun2 = Employee :: getName; System.out.println(fun2.apply(e)); }

转载地址:http://dtuki.baihongyu.com/

你可能感兴趣的文章
IF 语句
查看>>
循环语句
查看>>
DB2 临时表
查看>>
ITERATE、LEAVE、GOTO和RETURN
查看>>
异常处理
查看>>
存储过程
查看>>
动态SQL(Dynamic SQL)
查看>>
在存储过程之间传递数据
查看>>
迁移存储过程
查看>>
GET DIAGNOSTIC 语句
查看>>
Python 简介
查看>>
Python 注释
查看>>
Python 变量
查看>>
Python 数据类型 -- 数字
查看>>
Spring 管理对象
查看>>
Spring 自定义对象初始化及销毁
查看>>
Spring Batch 环境设置
查看>>
字符组转译序列
查看>>
字符转译序列
查看>>
Java 数据类型
查看>>