Lambda表达式改方法引用和构造器引用

方法引用

示例代码

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;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lambda表达式的条件构造器用于在Lambda表达式中根据条件来构造不同的结果。它可以根据给定的条件返回不同的表达式或语句。具体来说,Lambda表达式的条件构造器可以使用if语句或者三元运算符来实现条件判断。 使用if语句的Lambda表达式条件构造器的语法如下: (parameters) -> { if (condition) { // 执行条件为真的语句 return expression1; } else { // 执行条件为假的语句 return expression2; } } 使用三元运算符的Lambda表达式条件构造器的语法如下: (parameters) -> condition ? expression1 : expression2 注意,expression1和expression2可以是表达式或者语句块。条件构造器可以根据需要自由组合和嵌套,以实现更复杂的条件逻辑。 方法引用是对Lambda表达式符合某种情况下的一种缩写,使得我们的Lambda表达式更加精简。当Lambda表达式中的逻辑非常简单且只调用了一个已存在的方法时,可以考虑使用方法引用方法引用通过引用已存在的方法来替代Lambda表达式,使代码更加简洁和易读。方法引用的语法格式为:类名::方法名。 总结一下,Lambda表达式的条件构造器用于根据条件来构造不同的结果,可以使用if语句或者三元运算符来实现。方法引用是对符合某种情况下的Lambda表达式的一种缩写,用于替代Lambda表达式中调用已存在方法的情况,使代码更简洁。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java学习day042 lambda表达式构造器引用、变量作用域、处理lambda表达式、再谈Comparator)](https://blog.csdn.net/Zzehao11/article/details/105415027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java8新特性之lambda表达式(及方法引用构造器引用)](https://blog.csdn.net/cristianoxm/article/details/110222407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值