java方法引用详解

java方法引用详解_第1张图片

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 supplier=()->new Employee("Tom");
        Employee employee = supplier.get();
        System.out.println(employee);

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

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

        //数组引用
        Function 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 con1=str-> System.out.println(str);
        con1.accept("北京");

        PrintStream ps = System.out;
        Consumercon2=ps::println;
        con2.accept("上海");
    }

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

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

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

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

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


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

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

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

        Functionfunction1=Employee::getName;
        String apply1 = function1.apply(employee);
        System.out.println(apply1);

    }

}

你可能感兴趣的:(java,开发语言)