方法引用与构造方法引用

2024-06-03 05:20
文章标签 方法 引用 构造方法

本文主要是介绍方法引用与构造方法引用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

方法引用

什么是方法引用

构造方法引用

构造方法引用(也可以称作构造器引用)

数组构造方法引用


方法引用

什么是方法引用

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

方法引用可以看做是 Lambda 表达式深层次的表达。换句话说,方法引用就是 Lambda 表达式,也就是函数式接口的一个实例,通过方法的名字来指向一个方法,可以认为是 Lambda 表达式的一个语法糖。

要求:实现接口的抽象方法的参数类列和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致。

格式:使用操作符 :: 将类(或者对象)与方法名分割开来

如下三种主要使用情况:

  • 对象 :: 实例方法名
  • 类 :: 静态方法名
  • 类 :: 实例方法名

示例

public class Person {private String name;private LocalDate birthday;public Person() {}public Person(String name) {this.name = name;}public Person(String name, LocalDate birthday) {this.name = name;this.birthday = birthday;}public String getName() {return name;}public void setName(String name) {this.name = name;}public LocalDate getBirthday() {return birthday;}public void setBirthday(LocalDate birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", birthday=" + birthday +'}';}public static int compareByAge(Person a, Person b) {return a.birthday.compareTo(b.birthday);}
}
public class MethodReferences {//情况一:对象 :: 实例方法//Consumer 中的 void accept(T t) 与 PrintStream 中的 void println(T t)@Testpublic void t1() {//原来的写法Consumer<String> consumer = new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println(s);}};consumer.accept("上海市");//lambda 写法Consumer<String> consumerL = str -> System.out.println(str);consumerL.accept("上海");//方法引用PrintStream out = System.out;Consumer<String> consumerM = out::println;consumerM.accept("shanghai");}//Supplier 中的 T get() 与 Person 中的 String getName()@Testpublic void t2() {Person person = new Person("小威", LocalDate.of(2016, 9, 1));//原来的写法Supplier<String> supplier = new Supplier<String>() {@Overridepublic String get() {return person.getName();}};System.out.println(supplier.get());//lambda 写法Supplier<String> supplierL = () -> person.getName();System.out.println(supplierL.get());// 方法引用Supplier<String> supplierM = person::getName;System.out.println(supplierM.get());}
//======================================================================================================================//情况二:类 :: 静态方法//Comparator 中的 int compare(T t1,T t2) 与 Integer 中的 int compare(T t1,T t2)@Testpublic void t3() {//原来的写法Comparator<Integer> comparator = new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return Integer.compare(o1, o2);}};System.out.println(comparator.compare(520, 1314));//lambda 写法Comparator<Integer> comparatorL = (o1, o2) -> Integer.compare(o1, o2);System.out.println(comparatorL.compare(100, 99));// 方法引用Comparator<Integer> comparatorM = Integer::compareTo;System.out.println(comparatorM.compare(123, 321));}//Function 中 R apply(T t) 与 Math 中 Long round(Double d)@Testpublic void t4() {//原来的写法Function<Double, Long> function = new Function<Double, Long>() {@Overridepublic Long apply(Double aDouble) {return Math.round(aDouble);}};System.out.println(function.apply(13.7));//lambda 写法Function<Double, Long> functionL = d -> Math.round(d);System.out.println(functionL.apply(13.4));// 方法引用Function<Double, Long> functionM = Math::round;System.out.println(functionM.apply(13.9));}
//======================================================================================================================//情况三:类 :: 实例方法(有难度)//Comparator 中的 int compare(T t1,T t2) 与 String 中的 int t1.compareTo(t2)@Testpublic void t5() {//原来的写法Comparator<String> comparator = new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o1.compareTo(o2);}};System.out.println(comparator.compare("abc", "abd"));//lambda 写法Comparator<String> comparatorL = (o1, o2) -> o1.compareTo(o2);System.out.println(comparatorL.compare("abc", "abd"));// 方法引用Comparator<String> comparatorM = String::compareTo;System.out.println(comparatorM.compare("abd", "abc"));}//BiPredicate 中的 boolean test(T t1,T t2) 与 String 中的 boolean t1.equals(t2)@Testpublic void t6() {//原来的写法BiPredicate<String, String> biPredicate = new BiPredicate<String, String>() {@Overridepublic boolean test(String s, String s2) {return s.equals(s2);}};System.out.println(biPredicate.test("abc", "abd"));//lambda 写法BiPredicate<String, String> biPredicateL = (s1, s2) -> s1.equals(s2);System.out.println(biPredicateL.test("abc", "abd"));// 方法引用BiPredicate<String, String> biPredicateM = String::equals;System.out.println(biPredicateM.test("abc", "abd"));}//Function 中的 R apply(T t) 与 Person 中的 String getName()@Testpublic void t7() {Person person = new Person("Vincent", LocalDate.of(1991, 1, 28));//原来的写法Function<Person, String> function = new Function<Person, String>() {@Overridepublic String apply(Person person) {return person.getName();}};System.out.println(function.apply(person));//lambda 写法Function<Person, String> functionL = p -> p.getName();System.out.println(functionL.apply(person));// 方法引用Function<Person, String> functionM = Person::getName;System.out.println(functionM.apply(person));}
}

 

构造方法引用

构造方法引用又分构造方法引用和数组构造方法引用。

构造方法引用(也可以称作构造器引用)

组成语法格式:Class::new

构造函数本质上是静态方法,只是方法名字比较特殊,使用的是 new 关键字

要求:和方法引用类似,函数接口的抽象方法的形参列表和构造器的形参列表一致,且抽象方法的返回值类型即为构造器所属的类的类型。

示例:

String::new, 等价于 lambda 表达式 () -> new String()

public class ConstructorReferences {//构造器引用//Supplier 中的 T get() 与 Person()@Testpublic void t1() {//原来的写法Supplier<Person> supplier = new Supplier<Person>() {@Overridepublic Person get() {return new Person();}};System.out.println(supplier.get());//lambda的写法Supplier<Person> supplierL = () -> new Person();System.out.println(supplierL.get());//构造器引用Supplier<Person> supplierM = Person::new;System.out.println(supplierM.get());}//Function 中的 R apply(T t)@Testpublic void t2() {//原来的写法Function<String, Person> function = new Function<String, Person>() {@Overridepublic Person apply(String s) {return new Person(s);}};System.out.println(function.apply("小微"));//lambda的写法Function<String, Person> functionL = s -> new Person(s);System.out.println(functionL.apply("小微"));//构造器引用Function<String, Person> functionM = Person::new;System.out.println(functionM.apply("小微"));}//BiFunction 中的 apply(T t,U u)@Testpublic void t3() {//原来的写法BiFunction<String, LocalDate, Person> biFunction = new BiFunction<String, LocalDate, Person>() {@Overridepublic Person apply(String s, LocalDate localDate) {return new Person(s, localDate);}};Person person = biFunction.apply("威仔", LocalDate.of(2010, 12, 13));System.out.println(person);//lambda的写法BiFunction<String, LocalDate, Person> biFunctionL = (s, lD) -> new Person(s, lD);Person personL = biFunctionL.apply("威仔", LocalDate.of(2010, 12, 13));System.out.println(personL);//构造器引用BiFunction<String, LocalDate, Person> biFunctionM = Person::new;Person personM = biFunctionM.apply("威仔", LocalDate.of(2010, 12, 13));System.out.println(personM);}
}

 

数组构造方法引用

组成语法格式:TypeName[]::new

要求:可以把数组看做事一个特殊的类,则写法与构造器引用一致。

示例:

String[]::new 是一个含有一个参数的构造器引用,这个参数就是数组的长度。等价于 lambda 表达式 x -> new String[x]

public class ConstructorReferences {//数组引用//Function 中的 R apply(T t)@Testpublic void t4() {//原来的写法Function<Integer, String[]> function = new Function<Integer, String[]>() {@Overridepublic String[] apply(Integer integer) {return new String[integer];}};String[] apply = function.apply(10);System.out.println(Arrays.toString(apply));//lambda的写法Function<Integer, String[]> functionL = length -> new String[length];String[] applyL = functionL.apply(10);System.out.println(Arrays.toString(applyL));//数组引用Function<Integer, String[]> functionM = String[]::new;String[] applyM = function.apply(10);System.out.println(Arrays.toString(applyM));}
}

这篇关于方法引用与构造方法引用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1026112

相关文章

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行