Java中的方法引用的使用场景

方法引用的使用:

1.使用场景:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!

2. 方法引用本质上就是lambda 表达式,而Lambda式作为函数式接口的实例,所以方法引用也是函数式接口

3.使用格式: 类(或对象) :: 方法名

4. 具体分为如下的三种情况

    对象::非静态(实例方法)
    
    类:: 静态方法

    类::非静态方法

5.方法引用的使用要求: 接口中的抽象方法的形参列表和返回值类类型与方法引用的方法的形参列表和返回值类型 相同

使用方法引用的场景:

public class MethodquoteUse01 {
    
    
    @Test
    public void test(){
    
    
        // 情况一:对象::实例方法

        //Consumer 中的void accept(T t)
        //PrintStream 中的void println(T t)
        Consumer<String> consumer=s -> System.out.println(s);
            consumer.accept("在");

            // 使用方法引用
        System.out.println("*****");
        PrintStream out = System.out;
        Consumer<String> consumer1=out::println;
        consumer1.accept("在那里");

    }
    //Supplier 中的T get()
    // Employee 中的String getName()

    @Test
    public void test2(){
    
    
        Employee employee=new Employee(1,"陈涛");
        Supplier<String> stringSupplier=()->employee.getName();
        System.out.println(stringSupplier.get());

        System.out.println("**********");
        Supplier<String>stringSupplier1=employee::getName;
        System.out.println(stringSupplier1.get());

    }


    // 情况二:类::静态方法
    // Comparator 中的 int Compare(T t1,T t2)
    // Integer 中的int compare(T t1,T t2)
    @Test
    public void test3(){
    
    
        Comparator <Integer> comparator=(t1,t2)->Integer.compare(t1,t2);
        int compare = comparator.compare(12, 45);
        System.out.println(compare);

        // 使用方法的引用
        Comparator<Integer> comparator1=Integer::compareTo;
        System.out.println(comparator1.compare(45,40));

    }

    // Function中的R apply(T t)
    // Math中的Long round(Double d)
    @Test
    public void test4(){
    
    
        Function<Double,Long>function=d->Math.round(d);
        Long apply = function.apply(123.5);
        System.out.println(apply);

        //改造方法的引用
        Function<Double,Long> function1=Math::round;
        Long apply1 = function1.apply(12.55);
        System.out.println(apply1);
    }



    // 情况三: 类::实例方法(有难度)
    @Test
    public void test5(){
    
    
        // Comparator 中的int compare(T t1,T t2)
        // String 中的int t1.compareTo(t2)
    Comparator<String> comparator=(t1,t2)->t1.compareTo(t2);
        System.out.println(comparator.compare("abc","adc"));

        // 使用方法的引用
        Comparator<String>comparator1=String::compareTo;
        System.out.println(comparator1.compare("abc","adc"));


    }
    //BiPredicate 中的boolean test(T t1,T t2);
    //String中的boolean t1.equals(t2)

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

        // 使用方法的引用
        BiPredicate<String,String>predicate1=String::equals;
        System.out.println(predicate1.test("aa","aa"));
    }
    //Function 中的R apply(T t)
    //Employee 中的String getName();

    @Test
    public void test7(){
    
    
        // 使用Lambda表达式
        Employee employee1 = new Employee(12, "大大");
        Function<Employee,String> function=employee -> employee.getName();
        String apply = function.apply(employee1);
        System.out.println(apply);

        // 使用方法的引用
        Function<Employee,String> function1=Employee::getName;
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_46351306/article/details/114012909