Java新特性(四)-- 方法引用及内建式函数接口

1. 方法引用

我们都知道,引用肯定都是针对于引用类型(数组、类、接口)完成的。但是JDK1.8开始追加了方法引用的概念。实际上引用的本质是给面向对象中的方法起了个别名而已,所以方法的引用也就是别名的使用。

  • 而方法引用的类型有以下四种形式:
    ① 引用静态方法:类名称::静态方法名称
    ② 引用某个对象的方法:实例化对象 :: 普通方法
    ③ 引用某个特定类的方法:类名称 :: 普通方法
    ④ 引用构造方法:类名称 :: new

注意:方法引用一般结合函数式编程使用!

1.1 引用静态方法

String类的valueOf()方法是个静态方法。

package www.testdemo.lambda;

//函数泛型接口
@FunctionalInterface
interface IUtil1<P, R> {
    R switchPara(P p);
}

public class TestMethodRef {
    public static void main(String[] args) {
        IUtil1<Integer, String> iutil1 = (p) -> {
            return String.valueOf(p);
        };
        System.out.println(iutil1.switchPara(1));

        System.out.println("----------");

        //引用静态方法
        IUtil1<Integer, String> iutil2 = String::valueOf;
        //相当于调了String.switchPara(1)
        System.out.println(iutil2.switchPara(1));
    }
}

在这里插入图片描述

1.2 引用对象方法

String类的toUpperCase()方法为对象方法。

package www.testdemo.lambda;

@FunctionalInterface
interface IUtil2<R> {
    R convert();
}

public class TestMethodRef2 {
    public static void main(String[] args) {
        String str = "hello";

        IUtil2 iutil1 = () -> {
            return str.toUpperCase();
        };
        System.out.println(iutil1.convert());

        System.out.println("----------");
		
		//引用对象方法
        IUtil2<String> iutil2 = str::toUpperCase;
        System.out.println(iutil2.convert());
    }
}

在这里插入图片描述

1.3 引用类中普通方法

String类的compareTo()方法是个普通方法。

package www.testdemo.lambda;

@FunctionalInterface
interface IUtil3<P1, P2, R> {
    R compare(P1 p1, P2 p2);
}

public class TestMethodRef3 {
    public static void main(String[] args) {
        IUtil3<Integer, Integer, Integer> iutil1 = (p1, p2) -> {
            return p1.compareTo(p2);
        };
        System.out.println(iutil1.compare(2, 1));

        System.out.println("----------");
        
		//引用类中普通方法
        IUtil3<Integer, Integer, Integer> iutil2 = Integer::compareTo;
        System.out.println(iutil2.compare(2, 1));
    }
}

在这里插入图片描述

1.4 引用构造方法

package www.testdemo.lambda;

@FunctionalInterface
interface IUtil4<P1, P2, R> {
    R createObject(P1 p1, P2 p2);
}

class Person {

    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class TestMethodRef4 {
    public static void main(String[] args) {
        IUtil4<String, Integer, Person> iutil1 = (s, i) -> {
            return new Person(s, i);
        };
        System.out.println(iutil1.createObject("张三", 22));

        System.out.println("----------");
		
		//引用构造方法
        IUtil4<String, Integer, Person> iutil2 = Person::new;
        System.out.println(iutil2.createObject("张三", 22));
    }
}

在这里插入图片描述

2. 内建函数式接口

Lamdba的核心在于函数式接口。而函数式接口的核心是只能有一个方法。

  • java.util.function实际上将函数式编程分为以下四种类型:
    ① 功能型接口:public interface Function<T, R> R apply(T t);
    ② 供给型接口:public interface Supplier T get();
    ③ 消费型接口:public interface Consumer void accept(T t);
    ④ 断言型接口:public interface Predicate boolean test(T t);
package www.testdemo.lambda;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestFunction {
    public static void main(String[] args) {

        //功能型函数
        Function<Integer, String> function1 = String::valueOf;
        Function<String, Boolean> function2 = Boolean::parseBoolean;
        System.out.println(function1.apply(1));
        System.out.println(function2.apply("flase"));

        System.out.println("----------");

        //供给型函数
        Supplier<String> supplier1 = "hello"::toUpperCase;
        Supplier<Person> supplier2 = () -> {
            return new Person("张三", 22);
        };
        System.out.println(supplier1.get());
        System.out.println(supplier2.get());

        System.out.println("----------");

        //消费型函数
        Consumer<String> consumer = System.out :: println ;
        consumer.accept("hello");

        System.out.println("----------");

        //断言型函数
        Predicate<Person> predicate = (p) -> {
            return p != null;
        };
        System.out.println(predicate.test(new Person("张三", 22)));
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43508801/article/details/89019361