方法引用
示例代码
package com.debuggg.java.exer4;
import org.junit.Test;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* 作者 ZYL
* 功能描述 : 方法引用的使用
*
* 1.使用情景:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用
*
* 2.方法引用,本质上就是Lambda表达式,而Lambda表达式作为函数式接口的示例,所以方法引用也是函数式接口的实例
*
* 3.使用格式: 类(或对象) :: 方法名
*
* 4.具体分为如下三种情况:
* 对象 :: 非静态方法
* 类 :: 静态方法
*
* 类 :: 非静态方法
*
*5.方法引用使用的要求,要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表和返回值类型相同
* 日期 2020-03-01 15:06
* 参数 null
* 返回值
*/
public class MethodRefTest {
/**
* 作者 ZYL
* 功能描述 : 情况一:对象 :: 实例方法
* Consumer 中的 void accept(T t)
* PrintStream中的void println(T t)
*
* 日期 2020-03-01 15:06
* 参数 null
* 返回值
*/
@Test
public void test1(){
Consumer<String> con1 = s -> System.out.println(s);
con1.accept("北京");
System.out.println("******************************");
Consumer<String> con2 = System.out :: println;
con2.accept("beijing");
}
/**
* 作者 ZYL
* 功能描述 : Supplier中的T get()
* Employee中的String getName()
* 日期 2020-03-01 15:07
* 参数
* 返回值 void
*/
@Test
public void test2(){
List<Employee> employees = EmployeeData.getEmployees();
Supplier<String> supplier = () -> employees.get(0).getName();
System.out.println(supplier.get());
System.out.println("****************************");
//方法引用
Supplier<String> supplier1 = employees.get(0) :: getName;
System.out.println(supplier1.get());
}
/**
* 作者 ZYL
* 功能描述 : 情况二: 类 :: 静态方法
* Comparator中的int compare(T t1,T t2)
* Integer中的int compare(T t1,T t2)
* 日期 2020-03-01 17:10
* 参数
* 返回值 void
*/
@Test
public void test3(){
Comparator<Integer> comparator = (o1,o2) -> Integer.compare(o1,o2);
System.out.println(comparator.compare(10, 20));
Comparator<Integer> comparator1 = Integer :: compare;
System.out.println(comparator1.compare(10, 20));
}
/**
* 作者 ZYL
* 功能描述 : Function中的R apply(T t)
* Math中的Long round(Double d)
* 日期 2020-03-01 17:22
* 参数
* 返回值 void
*/
@Test
public void test4(){
Function<Double,Long> function = (d1) -> {
return Math.round(d1);
};
Function<Double,Long> function1 = Math :: round;
}
/**
* 作者 ZYL
* 功能描述 : 情况三:类 :: 实例方法
* Comparator中的int compare(T t1,T t2)
* String中的int t1.compareTo(t2)
*
*
*
* 日期 2020-03-01 17:38
* 参数
* 返回值 void
*/
@Test
public void test5(){
Comparator<String> comparator = (o1,o2) -> o1.compareTo(o2);
System.out.println("*******************");
//如果有2个参数,第一个参数做
Comparator<String> comparator2 = String::compareTo;
}
/**
* 作者 ZYL
* 功能描述 : BiPredicate中的boolean test(T t1,T t2)
* String 中的 boolean t1.equals(t2)
* 日期 2020-03-01 17:41
* 参数
* 返回值 void
*/
@Test
public void test6(){
BiPredicate<String,String> biPredicate = (s1,s2) -> s1.equals(s2);
System.out.println("**********************");
BiPredicate<String,String> biPredicate1 = String :: equals;
}
/**
* 作者 ZYL
* 功能描述 : Function 中的 R apply(T t)
* Employee中的String getName()
* 日期 2020-03-01 17:42
* 参数 null
* 返回值
*/
@Test
public void test7(){
List<Employee> employees = EmployeeData.getEmployees();
Employee employee = employees.get(0);
Function<Employee,String> function = (e) -> e.getName();
System.out.println(function.apply(employee));
System.out.println("******************");
Function<Employee,String> function1 = Employee::getName;
System.out.println(function1.apply(employee));
}
}
Employee的代码
package com.debuggg.java.exer4;
import java.util.Objects;
public class Employee {
private Integer id;
private String name;
private Integer age;
private Double salary;
public Employee() {
}
public Employee(Integer id, String name, Integer age, Double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(Integer id) {
this.id = id;
}
public Employee(Integer id,String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(id, employee.id) &&
Objects.equals(name, employee.name) &&
Objects.equals(age, employee.age) &&
Objects.equals(salary, employee.salary);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, salary);
}
}
EmployeeData的代码
package com.debuggg.java.exer4;
import java.util.ArrayList;
import java.util.List;
public class EmployeeData {
public static List<Employee> getEmployees(){
List<Employee> list = new ArrayList<>();
list.add(new Employee(1001,"马化腾",34,6000.38));
list.add(new Employee(1002,"马云",12,9876.12));
list.add(new Employee(1003,"刘强东",33,3000.82));
list.add(new Employee(1004,"雷军",26,7567.37));
list.add(new Employee(1005,"李彦宏",65,5555.32));
list.add(new Employee(1006,"比尔盖茨",42,9500.43));
list.add(new Employee(1007,"任正非",26,4333.32));
list.add(new Employee(1008,"扎克伯格",35,2500.32));
return list;
}
}
构造器引用
示例代码
package com.debuggg.java.exer4;
import org.junit.Test;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* 作者 ZYL
* 功能描述 :
* 一、构造器引用
* 和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一直,抽象方法的返回值类型即为构造器所属的类的类型
*
* 二、数组引用
* 把数组看做是一个特殊的类,则写法与构造器引用一致
*
*
* 日期 2020-03-01 17:58
* 参数 null
* 返回值
*/
public class ConstructorRefTest {
/**
* 作者 ZYL
* 功能描述 : 构造器引用
* Supplier中的T get()
* 日期 2020-03-01 17:59
* 参数
* 返回值 void
*/
@Test
public void test1(){
Supplier<Employee> sup1 = () -> new Employee();
System.out.println("**************");
Supplier<Employee> sup2 = Employee::new;
}
/**
* 作者 ZYL
* 功能描述 : Function 中的R apply(T t)
* 日期 2020-03-01 18:00
* 参数
* 返回值 void
*/
@Test
public void test2(){
Function<Integer,Employee> function = (id) -> new Employee(id);
System.out.println("**********");
Function<Integer,Employee> function1 = Employee::new;
}
/**
* 作者 ZYL
* 功能描述 : BiFunction中的R apply(T t,U u)
* 日期 2020-03-01 18:00
* 参数
* 返回值 void
*/
@Test
public void test3(){
BiFunction<Integer,String,Employee> biFunction = (int1,s1) -> new Employee(int1,s1);
System.out.println(biFunction.apply(1001,"张三"));
System.out.println("***************************");
BiFunction<Integer,String,Employee> biFunction1 = Employee::new;
System.out.println(biFunction.apply(1002,"李四"));
}
/**
* 作者 ZYL
* 功能描述 : 数组引用: Function 中的R apply(T t)
* 日期 2020-03-01 18:00
* 参数
* 返回值 void
*/
@Test
public void test4(){
Function<Integer,String[]> function = length -> new String[length];
String[] apply = function.apply(5);
System.out.println("********");
// 改为数组引用,
Function<Integer,String[]> function2 = String[] :: new;
}
}
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载