JDK1.8新特性 深入理解 (一)

本文详细介绍了Java中的四大核心函数式接口:Consumer、Supplier、Function和Predicate的功能与使用方法,并展示了如何自定义函数式接口及方法引用的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、函数式接口

函数式接口的提出是为了给Lambda表达式的使用提供更好的支持。
什么是函数式接口?
简单来说就是只定义了一个抽象方法的接口(Object类的public方法除外),就是函数式接口,并且还提供了注解:@FunctionalInterface

1、四大函数式接口Consumer、Supplier、Function和Predicate

它们之间有哪些区别呢?
(1)、Consumer 《T》:消费型接口,有参无返回值
(2)、Supplier 《T》:供给型接口,无参有返回值
(3)、Function 《T,R》::函数式接口,有参有返回值
(4)、Predicate《T》: 断言型接口,有参有返回值,返回值是boolean类型

(1) Consumer 《T》消费型接口
@Test
public void test(){
    changeStr("hello",(str) -> System.out.println(str));
}
public void changeStr(String str, Consumer<String> con){
    con.accept(str);
}
(2)、 Function 《T,R》函数式接口
@Test
public void test3(){
    Long result = changeNum(100L, (x) -> x + 200L);
    System.out.println(result);
}
public Long changeNum(Long num, Function<Long, Long> fun){
    return fun.apply(num);
}
(3)、Supplier 《T》供给型接口
@Test
public void test2(){
    String value = getValue(() -> "hello");
    System.out.println(value);
}
public String getValue(Supplier<String> sup){
    return sup.get();
}
(4)、predicate《T》断言型接口
public void test4(){
    boolean result = changeBoolean("hello", (str) -> str.length() > 5);
    System.out.println(result);
}
public boolean changeBoolean(String str, Predicate<String> pre){
    return pre.test(str);
}

在四大核心函数式接口基础上,还提供了诸如BiFunction、BinaryOperation、toIntFunction等扩展的函数式接口,都是在这四种函数式接口上扩展而来的,不做赘述。
总结:函数式接口的提出是为了让我们更加方便的使用lambda表达式,不需要自己再手动创建一个函数式接口,直接拿来用就好了。

2、自定义函数式接口

/**
 * 定义接口
 */
@FunctionalInterface
public interface Formula<F,T> {
    T calculate(F a);
    default double sqrt(double number) {
        return Math.sqrt(number);
    }
}

/**
 * 函数式接口调用
 */
public class Main {

    public static void main(String args[]){
        /**
         * 普通方法实现
         */
        Formula<Double,Double> formula1=new Formula<Double, Double>() {
            @Override
            public Double calculate(Double a) {
                return sqrt(1*10);
            }
        };
        double number1=formula1.calculate(100d);
        double number2=formula1.sqrt(16);
        System.out.println(number1);
        System.out.println(number2);

        /**
         * 使用Lambda表达式来进行实现
         */
        //访问局部变量,final可以不写,但是它隐式的是final,并且不可被修改
        final int temp=3;
        //Formula<String,Double> formula2=Double::valueOf;
        Formula<String,Double> formula2=number->Double.valueOf(number)+3;
        double num=formula2.calculate("1000");
        System.out.println(num);    
    }
}

二、方法引用

1、基本方法使用

三种表现形式:
(1)、对象::实例方法名
(2)、类::静态方法名
(3)、类::实例方法名 (lambda参数列表中第一个参数是实例方法的调用 者,第二个参数是实例方法的参数时可用)

Consumer<Integer> con = (x) -> System.out.println(x);
con.accept(100);

// 方法引用-对象::实例方法
Consumer<Integer> con2 = System.out::println;
con2.accept(200);

// 方法引用-类名::静态方法名
BiFunction<Integer, Integer, Integer> biFun = (x, y) -> Integer.compare(x, y);
BiFunction<Integer, Integer, Integer> biFun2 = Integer::compare;
Integer result = biFun2.apply(100, 200);

// 方法引用-类名::实例方法名
BiFunction<String, String, Boolean> fun1 = (str1, str2) -> str1.equals(str2);
BiFunction<String, String, Boolean> fun2 = String::equals;
Boolean result2 = fun2.apply("hello", "world");
System.out.println(result2);

2、构造器引用

public class Person {
    String fullName;
    String firstName;
    String lastName;
    Person(String fullName) {
        this.fullName=fullName;
    }
    Person() {}
    Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
public interface PersonFactory<P extends Person> {
    P create(String firstName, String lastName);
}
public class Main {
    public static void main(String args[]){
        /**
         * 构造器的实现以及原理
         */
        PersonFactory<Person> personPersonFactory= Person::new;
        personPersonFactory.create("zhang","zhaoliang");

        // 构造方法引用  类名::new
        Supplier<Person> sup = () -> new Person();
        System.out.println(sup.get());
        Supplier<Person> sup2 = Person::new;
        System.out.println(sup2.get());

        // 构造方法引用 类名::new (带一个参数)
        Function<String, Person> fun = (f) -> new Person(f);
        Function<String, Person> fun2 = Person::new;
        System.out.println(fun2.apply("zhangzhaoliang"));
    }
}

3、数组引用

 // 数组引用
 Function<Integer, String[]> fun = (x) -> new String[x];
 Function<Integer, String[]> fun2 = String[]::new;
 String[] strArray = fun2.apply(10);
 Arrays.stream(strArray).forEach(System.out::println);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值