4、jdk8-函数式接口简单使用

本文深入探讨Java函数式接口的精要,包括Predicate、Function、Supplier、Consumer及Comparator等接口的实际应用,通过实例讲解如何利用lambda表达式简化代码,提升编程效率。

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

本内容取自官方示例,并加以小测试使用

1、函数接口说明

函数式接口的定义:

  1. 有一个抽象方法的普通接口就是函数式接口(静态与默认方法不算),默认继承object,覆盖父类方法也不算;
  2. 任何函数式接口都可以使用lambda表达式替换
  3. 这个注解可存在可不存在,有定义多于一个方法时存在则出错
@FunctionalInterface
interface Fun {
    void foo();
    //void wait();出错
}

2、Predicate接口

用于判断的接口(直接引用函数、表达式)

public static void testPredicate() {

    Predicate<String> predicate = (s) -> s.length() > 0;
    predicate.test("foo");              // true
    predicate.negate().test("foo");     // false

    boolean flag = false; //判断布尔值变量
    Predicate<Boolean> nonNull = Objects::nonNull;
    Predicate<Boolean> isNull = Objects::isNull;
    log(nonNull.test(flag));//true

    Person pp = new Person("abc,", "efg");
    Predicate<String> isEmpty = String::isEmpty;
    log(isEmpty.test(pp.firstName));//false
    Predicate<String> isNotEmpty = isEmpty.negate();

    Predicate<Person> isEqual = e -> "abc".equals(e.firstName);
    log("对象值判断:" + isEqual.test(new Person("abc", "efg")));//true
}

3、Function接口

接收一个参数并返回一个结果,并提供一些其他函数可以使用的附加信息


public static void testFunction() {
    // Functions
    Function<String, Integer> toInteger = Integer::valueOf;
    Function<String, String> backToString = toInteger.andThen(String::valueOf);
    log(backToString.apply("123"));     // "123"

    Function<String, String> toFun1 = Lambda3::funTest1;
    Function<String, String> toFun2 = Lambda3::funTest2;
    log(toFun1.apply("mmo"));

    //compose 函数先执行参数,然后执行调用者,而 andThen 先执行调用者,然后再执行参数。
    Function<String, String> toFun3 = toFun1.andThen(toFun2);
    log(toFun3.apply("xxmmo"));//test2 fun -->test fun -->xxmmo
}


public static String funTest1(String val) {
    return "test fun -->" + val;
}

public static String funTest2(String val) {
    return "test2 fun -->" + val;
}

4、Supplier接口

提供者接口简单使用,返回任意类型值,与Function区别是没有参数

//每次调用get()方法的时候才会创建对象。并且每次调用创建的对象都不一样;
public static void testSupplier() {
    // Suppliers
    Supplier<Person> personSupplier = Person::new;
    log(personSupplier.get());// new Person
    log(personSupplier.get());

    Supplier<String> strTest1 = String::new;
    log(strTest1.get());//""
}

5、Consumer、Comparator等接口

其他额外几个接口的使用

public static void testOther() throws Exception{

    // Consumers:定义一个参数,对其进行(消费)处理,处理的方式可以是任意操作.
    Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);//赋值函数
    greeter.accept(new Person("Luke", "Skywalker"));//调用


    // Comparators:比较
    Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName);
    Person p1 = new Person("John", "Doe");
    Person p2 = new Person("Alice", "Wonderland");

    comparator.compare(p1, p2);             // > 0
    comparator.reversed().compare(p1, p2);  // < 0


    // Runnables
    Runnable runnable = () -> System.out.println(UUID.randomUUID());//相当于编写run方法
    runnable.run();


    // Callables
    Callable<UUID> callable = UUID::randomUUID;
    callable.call();
}

6、简单调用
public static void main(String[] args) throws Exception {
    testPredicate();
    testFunction();
    testSupplier();

    testOther();
}

public static void log(Object val) {
    System.out.println(val);
}

更多请参考
官方

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值