Java中方法引用-lambda的另外一种表现形式

方法引用:

若lambda 体重的内容有方法已经实现了,我们可以使用”方法引用”,

方法引用可以理解为lambda表达式的另外一种表现形式

主要的三种语法格式,外加一种构造器引用,数组引用

对象::实例方法名 类::静态方法名 类::实例方法名 类::new Type[]::new

需要注意的是:

1.方法引用的中方法的参数类型和返回值需要和函数式接口中的抽象方法参数类型和返回值保持一致

2. 通过类名调用实例方法,lambda表达式有两个参数,当第一个参数是此方法的调用者,第二个参数是此方法的参数,则可以使用形式

3. 构造器引用,需要调用的构造器的参数列表需要与函数式接口中抽象方法的函数列表保持一致

对象::实例方法名

    @Test
    public void test1() {

        System.out.println("------lambda表达式的常见情况-------");
        Consumer<String> consumer1 = (x) -> System.out.println(x);
        consumer1.accept("hello world");
        System.out.println();

        System.out.println("-------使用方法的引用1----");
        PrintStream printStream = System.out;
        Consumer<String> consumer2 = (x) -> printStream.println(x);
        consumer2.accept("hello world");
        System.out.println();

        System.out.println("--------使用方法的引用2-----");
        Consumer<String> consumer3 = System.out::println;
        consumer3.accept("hello world");
        System.out.println();
    }

    @Test
    public void test2() {

        System.out.println("----------lambda表达式的常见情况--------");
        Employee employee = new Employee("imekaku", 10);
        Supplier<String> supplier = () -> employee.getName();
        System.out.println(supplier.get());
        System.out.println();

        System.out.println("-----------使用方法引用------------");
        Supplier<Integer> supplier1 = employee::getAge;
        System.out.println(supplier1.get());
        System.out.println();
    }

类::静态方法名

    @Test
    public void test3() {


        Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);

        Comparator<Integer> comparator1 = Integer::compare;

        Set<Integer> set = new TreeSet<>(comparator);
        set.add(123);
        set.add(345);
        set.add(111);
        set.add(222);
        for (Integer i : set) {
            System.out.print(i + " ");
        }
        System.out.println();

        Set<Integer> set1 = new TreeSet<>(comparator1);
        set1.add(123);
        set1.add(345);
        set1.add(111);
        set1.add(222);
        for (Integer i : set1) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

类::实例方法名

    @Test
    public void test4() {
        System.out.println("---------lambda表达式------------");
        BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y);
        System.out.println(biPredicate.test("hello world", new String("hello world")));

        // 通过类名调用实例方法,
        // lambda表达式有两个参数,当第一个参数是此方法的调用者,第二个参数是此方法的参数,则可以使用形式
        System.out.println("---------使用方法的引用----------");
        BiPredicate<String, String> biPredicate1 = String::equals;
        System.out.println(biPredicate1.test("hello world", new String("hello world")));
    }

类::new

    @Test
    public void test5() {

        System.out.println("----------lambda表达式---------");
        Supplier<Employee> supplier = () -> new Employee("imekaku", 10);
        System.out.println(supplier.get());

        System.out.println("-----------使用方法的引用----------");
        Supplier<Employee> supplier1 = Employee::new;
        System.out.println(supplier1.get());
    }

    @Test
    public void test6() {

        System.out.println("----------lambda表达式---------");
        BiFunction<String, Integer, Employee> biFunction = (name, age) -> new Employee(name, age);
        System.out.println(biFunction.apply("imekaku", 10));

        System.out.println("-----------使用方法的引用---------");
        BiFunction<String, Integer, Employee> biFunction1 = Employee::new;
        System.out.println(biFunction1.apply("imekaku", 10));
    }

Type[]::new

    @Test
    public void test7() {

        System.out.println("--------lambda表达式-----------");
        Function<Integer, String[]> function = (x) -> new String[x];
        String[] strArray = function.apply(10);
        System.out.println(strArray.length);

        System.out.println("----------使用方法引用-----------");
        Function<Integer, String[]> function1 = String[]::new;
        String[] strArray1 = function1.apply(10);
        System.out.println(strArray1.length);
    }

完整代码示例

package com.imekaku;

import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.*;

/**
 * Created by lee on 2017/6/9.
 */
public class TestLambda5 {

    @Test
    public void test1() {

        System.out.println("------lambda表达式的常见情况-------");
        Consumer<String> consumer1 = (x) -> System.out.println(x);
        consumer1.accept("hello world");
        System.out.println();

        System.out.println("-------使用方法的引用1----");
        PrintStream printStream = System.out;
        Consumer<String> consumer2 = (x) -> printStream.println(x);
        consumer2.accept("hello world");
        System.out.println();

        System.out.println("--------使用方法的引用2-----");
        Consumer<String> consumer3 = System.out::println;
        consumer3.accept("hello world");
        System.out.println();
    }

    @Test
    public void test2() {

        System.out.println("----------lambda表达式的常见情况--------");
        Employee employee = new Employee("imekaku", 10);
        Supplier<String> supplier = () -> employee.getName();
        System.out.println(supplier.get());
        System.out.println();

        System.out.println("-----------使用方法引用------------");
        Supplier<Integer> supplier1 = employee::getAge;
        System.out.println(supplier1.get());
        System.out.println();
    }

    @Test
    public void test3() {


        Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);

        Comparator<Integer> comparator1 = Integer::compare;

        Set<Integer> set = new TreeSet<>(comparator);
        set.add(123);
        set.add(345);
        set.add(111);
        set.add(222);
        for (Integer i : set) {
            System.out.print(i + " ");
        }
        System.out.println();

        Set<Integer> set1 = new TreeSet<>(comparator1);
        set1.add(123);
        set1.add(345);
        set1.add(111);
        set1.add(222);
        for (Integer i : set1) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    @Test
    public void test4() {
        System.out.println("---------lambda表达式------------");
        BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y);
        System.out.println(biPredicate.test("hello world", new String("hello world")));

        // 通过类名调用实例方法,
        // lambda表达式有两个参数,当第一个参数是此方法的调用者,第二个参数是此方法的参数,则可以使用形式
        System.out.println("---------使用方法的引用----------");
        BiPredicate<String, String> biPredicate1 = String::equals;
        System.out.println(biPredicate1.test("hello world", new String("hello world")));
    }

    @Test
    public void test5() {

        System.out.println("----------lambda表达式---------");
        Supplier<Employee> supplier = () -> new Employee("imekaku", 10);
        System.out.println(supplier.get());

        System.out.println("-----------使用方法的引用----------");
        Supplier<Employee> supplier1 = Employee::new;
        System.out.println(supplier1.get());
    }

    @Test
    public void test6() {

        System.out.println("----------lambda表达式---------");
        BiFunction<String, Integer, Employee> biFunction = (name, age) -> new Employee(name, age);
        System.out.println(biFunction.apply("imekaku", 10));

        System.out.println("-----------使用方法的引用---------");
        BiFunction<String, Integer, Employee> biFunction1 = Employee::new;
        System.out.println(biFunction1.apply("imekaku", 10));
    }

    @Test
    public void test7() {

        System.out.println("--------lambda表达式-----------");
        Function<Integer, String[]> function = (x) -> new String[x];
        String[] strArray = function.apply(10);
        System.out.println(strArray.length);

        System.out.println("----------使用方法引用-----------");
        Function<Integer, String[]> function1 = String[]::new;
        String[] strArray1 = function1.apply(10);
        System.out.println(strArray1.length);
    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部