java方法引用详解

1.构造器引用和数组引用

package src.com.zhang.lambda;

import org.junit.Test;

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

/**
 * 构造器引用
 * 数组引用
 */
public class ConstructorRefTest {

    //构造器引用
    @Test
    public void test1(){
        Supplier<Employee> supplier=()->new Employee("Tom");
        Employee employee = supplier.get();
        System.out.println(employee);

        //简写
        Supplier<Employee>supplier1=Employee::new;
        Employee employee1 = supplier1.get();
        System.out.println(employee1);

        //Function中的apply
        Function<String,Employee>function=name->new Employee(name);
        Employee tim = function.apply("Tim");
        System.out.println(tim);
        //简写
        Function<String,Employee>function1=Employee::new;
        Employee jack = function1.apply("Jack");
        System.out.println(jack);

        //数组引用
        Function<Integer,String[]> function2=length->new String[length];
        String[] apply = function2.apply(3);
        apply[0]="1";
        System.out.println(Arrays.toString(apply));

    }

}

方法引用的几种情况:

package src.com.zhang.lambda;

import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;


/**
 * 方法引用使用的条件:当要传递给lambda的操作,已经有了实现的方法,要求接口中的抽象方法的形参列表和返回值类型与方法引用中的都相同,
 */

public class MethodReference {
    //情况一:对象::实例方法

    /**
     * 消费型接口的方法引用
     */
    @Test
    public void test1(){
        Consumer<String> con1=str-> System.out.println(str);
        con1.accept("北京");

        PrintStream ps = System.out;
        Consumer<String>con2=ps::println;
        con2.accept("上海");
    }

    /**
     * 供给型接口的方法引用
     */
    @Test
    public void test2(){
        Employee employee=new Employee("Tom");
        Supplier<String> sup1=()->employee.getName();
        String s = sup1.get();
        System.out.println(s);

        Supplier<String> sup2=employee::getName;
        String s1 = sup2.get();
        System.out.println(s1);
    }

    //类::静态方法
    @Test
    public void test3(){
        Comparator<Integer> com1=(o1,o2)->Integer.compare(o1,o2);
        int compare = com1.compare(2, 2);
        System.out.println(compare);
        //简写:
        Comparator<Integer> com2=Integer::compareTo;
        int compare1 = com2.compare(2, 22);
        System.out.println(compare1);

        //Fuction接口实现四舍五入
        Function<Double,Long>func1=d->Math.round(d);
        Long apply = func1.apply(13.3);
        System.out.println(apply);
        //简写
        Function<Double,Long>func2=Math::round;
        Long apply1 = func2.apply(444.9);
        System.out.println(apply1);
    }

    //类::实例方法(非静态方法)
    @Test
    public void test4(){
        Comparator<String> com1=(s1,s2)->s1.compareTo(s2);
        int compare = com1.compare("ac", "ad");
        System.out.println(compare);
        //方法引用
        Comparator<String> com2=String::compareTo;
        int compare1 = com2.compare("ac", "ad");
        System.out.println(compare1);


        BiPredicate<String,String> pre1=(s1,s2)->s1.equals(s2);
        boolean test = pre1.test("abc", "abc");
        System.out.println(test);

        BiPredicate<String,String> pre2=String::equals;
        boolean test1 = pre2.test("aaa", "aaa");
        System.out.println(test1);

        Employee employee=new Employee("Jack");
        Function<Employee,String>function=e->e.getName();
        String apply = function.apply(employee);
        System.out.println(apply);

        Function<Employee,String>function1=Employee::getName;
        String apply1 = function1.apply(employee);
        System.out.println(apply1);

    }

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值