当前位置:网站首页>Method reference instance method reference
Method reference instance method reference
2022-06-12 15:18:00 【A_ M amu】
The only difference between it and static method reference : Use the instance method called by the object .
package demo01;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.function.Supplier;
/**
* @program: jdk8-02
* @description:
* @author: Amur
* @create: 2022-02-15 08:46
**/
public class Test01 {
public static void main(String[] args) {
Student s=new Student(" Zhang San ",18);
// Use supplied function interface T get();
Supplier<String> supplier=()->s.getName();
System.out.println(supplier.get());
//(args)-> Object name . Example method (args); --- object :: Example method
Supplier<String> supplier1=s::getName;
System.out.println(supplier1.get());
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Student{
private String name;
private Integer age;
}Object method reference
if Lambda The first parameter in the parameter list is the parameter caller of the instance method , And the second argument is the argument to the instance method , You can use object methods to reference .
package demo02;
import java.util.function.BiPredicate;
/**
* @program: jdk8-02
* @description:
* @author: Amur
* @create: 2022-02-15 08:52
**/
public class Test02 {
public static void main(String[] args) {
BiPredicate<String,String> predicate=(t1,t2)->t1.equals(t2);
System.out.println(predicate.test("hello", "Hello"));
//(arg1,arg2)-->agr1. Example method (arg2);----- Class name :: Example method .
BiPredicate<String,String> predicate1=String::equals;
System.out.println(predicate1.test("hello","hello"));
}
}
Construction method reference
Be careful : The argument list of the constructor to be invoked is consistent with the argument list of the abstract method in the functional interface .
package demo03;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.function.BiFunction;
import java.util.function.Supplier;
/**
* @program: jdk8-02
* @description:
* @author: Amur
* @create: 2022-02-15 09:04
**/
public class Test03 {
public static void main(String[] args) {
Supplier<Student> supplier=()->new Student();
System.out.println(supplier.get());
// Conform to construction method reference
Supplier<Student> supplier1=Student::new;
System.out.println(supplier1.get());
BiFunction<String,Integer,Student> function=( t, u)->new Student(t,u);
System.out.println(function.apply(" Zhang San ",20));
BiFunction<String,Integer,Student> function1=Student::new;
System.out.println(function1.apply(" Li Si ",19));
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Student{
private String name;
private Integer age;
}
边栏推荐
- 虚拟机中用户和root忘记密码解决办法
- [spark][core] what is an external shuffle service?
- Pointer related concepts
- Autofac (2)
- 宝塔面板新建数据库提示数据库名不能大于16位的解决方法
- 如何写年终总结
- USART(RS232422485)、I2C、SPI、CAN、USB总线
- Learning is an inhumane thing (becoming an expert's internal mind skill)
- 3D reconstruction system | L3 dual view motion recovery structure (SFM binocular SFM)
- [SPARK][CORE] 面试问题之谈一谈Push-based shuffle
猜你喜欢

Multi thread knowledge induction

解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击

Solve log4j2 vulnerability and be attacked by mining and zombie process viruses

SOA Architecture

Array related content

TF learning notes in ROS

增加mysql的最大连接数
![[jvm learning] parental delegation mechanism and PC register (program counter)](/img/23/c7b1de7f648716c0f6b615dd22c64d.jpg)
[jvm learning] parental delegation mechanism and PC register (program counter)

Scala download and idea installation of scala plug-ins (nanny level tutorial is super detailed)
![[LDA] rough version notes of EM variational reasoning [to be improved](/img/de/4b028e77694297db05a2d6b6589276.png)
[LDA] rough version notes of EM variational reasoning [to be improved
随机推荐
同花顺手机炒股开户安全吗
NETCORE combined with cap event bus to realize distributed transaction -- Introduction (1)
C escape character
Alibaba, Tencent et pingduo sont à la recherche d'une nouvelle logique pour l'Internet industriel
Browser fingerprint interpretation
Industrial end: a new battlefield of 618
C scanf function
Notes on ARM 64 instructions
h3c GR5200路由器上如何设置公网ip可以访问
[game server design cases] insights
Deepin20.6 rtx3080 installing graphics card drivers 510.60.02, cuda11.6, pytorch1.11
C语言打开中文路径文件
Servlet connects to database to realize user login function
New features of ES6
Seaborn的简述
[jvm learning] types of GC and allocation process of objects on JVM heap
First set and follow set in vernacular
Introduction to resttemplate
Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
jupyter notebook新环境快捷方式