JDK8-第二章:方法引用表达式

方法引用和构造器引用

方法引用(Method References)
●当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
● 方法引用可 以看做是Lambda表达式深层次的表达。 换句话说,方法引用就
是L&mbda表达式,也就是函数式接口的-一个实例,通过方法的名字来指向
一个方法,可以认为是Lambda表达式的一一个 语法糖。
●要求:实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的
方法的参数列表和返回值类型保持一致!
格式:使用操作符“::” 将类(或对象)与方法名分隔开来。
●如下三种主要使用情况:
➢对象::实例方法名
➢类::静态方法名;
➢类::实例方法名

使用

*方法引用的使用

1.使用情境: 当要传递给Lambda体的操作, 已经有实现的方法了, 可以使用方法引用!
2.方法引用,本质上就是Lambda表达式, 而Lambda表达式作为函数式接口的实例。所以
方法引用,也是函数式接口的实例。
3.使用格式: 类(或对象) ::方法名
4.具体分为如下的三种情况:
对象::非静态方法
类::静态方法
类::非静态方法
5.方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的
形参列表和返回值类型相同!

package com.changxiong.citeTest;

/**
 * @author changxiong
 * @create 2020-12-14-11:18 PM
 */
public class Employee {
    private String id;
    private String name;
    private Integer age;
    private Integer salary;

    public Employee() {
    }

    public Employee(String id) {
        this.id = id;
    }

    public Employee(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public Employee(String id, String name, Integer age, Integer salary) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getId() {
        return id;
    }

    public void setId(String 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 Integer getSalary() {
        return salary;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }
}

package com.changxiong.citeTest;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author changxiong
 * @create 2020-12-14-9:07 PM
 *
 * 引用模式
 */
public class citeDemo {
    public static void main(String[] args) {
        Consumer<String> consumer=c-> System.out.println(c);
        consumer.accept("aaa");

        System.out.println();

        Consumer<String> c1=System.out::println;
        c1.accept("aaaa");
    }


    @Test
    public void demo(){
        Employee emp1 = new Employee("123", "tom", 10, 100);

        Supplier<String> s=()->emp1.getName();

        System.out.println(s.get());

        System.out.println();

        Supplier<String> s1=emp1 :: getName;

        System.out.println(s1.get());
    }

    //传两个参数
    @Test
    public void demo1() {
        Comparator<Integer> c = (c1, c2) -> c1.compareTo(c2);
        System.out.println(c.compare(10,11));

        System.out.println();

        Comparator<Integer> comparator= Integer::compareTo;
        System.out.println(comparator.compare(10, 11));
    }

    @Test
    public void demo2() {
        Function<String,Integer> f1=s->s.length();

        System.out.println(f1.apply("aaaa"));

        System.out.println();

        Function<String,Integer> f2= String :: length;
        System.out.println(f2.apply("ssss"));

        System.out.println();

        Function<Double, Double> f3 = s -> Math.floor(s);
        System.out.println(f3.apply(3.5));

        System.out.println();

        Function<Double,Double> f4=Math::floor;

        System.out.println(f4.apply(5.5));

    }

    @Test
    public void demo3() {
        BiPredicate<String,String> p=(s1,s2)->s1.equals(s2);
        System.out.println(p.test("aa", "bb"));

        System.out.println();
        BiPredicate<String,String> p1=String::equals;
        System.out.println(p1.test("aa", "bb"));


    }
}

数组引用

package com.changxiong.citeTest;

import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author changxiong
 * @create 2020-12-15-10:09 AM
 *
 * 数组引用
 */
public class ArrayCite {

    @Test
    public void test1() {
        Supplier<Employee> e1 = () -> new Employee("1","xiaoming",10,100);

        System.out.println(e1.get().getName());

        System.out.println();

        Supplier<Employee> e2=Employee :: new;
        System.out.println(e2.get());

    }

    @Test
    public void test2() {
        Function<String,Employee> f1=id->new Employee(id);
        Employee apply = f1.apply("10");

        System.out.println(apply.getId());

        System.out.println();

        Function<String,Employee> f2= Employee::new;
        Employee apply1 = f2.apply("10");

        System.out.println(apply1.getId());
    }

    @Test
    public void test3() {
        BiFunction<String, String, Employee> f1 = (id, name) -> new Employee(id, name);
        System.out.println(f1.apply("10","tom").getId());

        System.out.println();

        BiFunction<String, String, Employee> f2=Employee::new;
        System.out.println(f2.apply("12","jack").getName());

    }

    @Test
    public void test4() {
        Function<Integer, String[]> f1 = length -> new String[length];
        System.out.println(Arrays.toString(f1.apply(10)));

        System.out.println();

        Function<Integer,String[]> f2=String[]::new;
        System.out.println(Arrays.toString(f2.apply(5)));
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值