Java8四大函数式接口

四个主要的函数式接口

这些接口是在java.util.function包下的,这些接口都是函数式接口,所以可以使用lambda表达式来创建这些接口的对象。

接口的介绍和快速使用

  1. Consumer<T>: 该接口代表接受一个输入参数并且不返回任何结果的操作。它主要用于操作对象。

  2. Supplier<T>: 该接口不接受任何参数,它返回一个泛型的值。通常用于提供新的对象。

  3. Function<T, R>: 该接口接受一个输入参数,返回一个结果。这是一个功能性接口,其用途是将输入对象转换成不同类型的输出对象。

  4. Predicate<T>: 该接口接受一个输入参数,返回一个布尔值结果。这主要用于测试对象的某些属性。

假设我们有一个简单的类 Person

public class Person {
    private String name;
    private int age;

    // 构造函数、getters和setters省略
}

Supplier 接口示例

接口的抽象方法

这里,Supplier提供了一个新的Person实例。

Supplier<Person> personSupplier=()->new Person("Alice",30);
        Person p=personSupplier.get();

Supplier 接口不包含默认方法。

Consumer 接口示例

接口的抽象方法

这里,我们创建了一个Consumer来打印一个人的名字。

Consumer<Person> greeter=(p)->System.out.println("Hello, "+p.getName());
        greeter.accept(new Person("Alice",30));

接口的默认方法

Consumer 接口表示执行在单个输入参数上的操作。它的主要默认方法是:

  • andThen(Consumer<? super T> after): 这个默认方法允许你在一个 Consumer 之后执行另一个 Consumer
    。这对于创建一系列操作是非常有用的。

    Consumer<Person> greetConsumer = person -> System.out.println("Hello, " + person.getName());
    Consumer<Person> byeConsumer = person -> System.out.println("Goodbye, " + person.getName());
    
    Consumer<Person> combinedConsumer = greetConsumer.andThen(byeConsumer);
    combinedConsumer.accept(new Person("Alice", 25)); // 先打印问候,然后打印告别
    //  等于是
    //  greetConsumer.andThen(byeConsumer).accept(new Person("Alice", 25)); // 先打印问候,然后打印告别
    

Function 接口示例

接口的抽象方法

这个Function接受一个Person对象并返回其名字。

Function<Person, String> nameFunction=(Person p)->p.getName();
        String name=nameFunction.apply(new Person("Alice",30));

接口的默认方法

Function 接口表示接受一个参数并产生结果的函数。它包含几个有用的默认方法:

  • andThen(Function<? super R,? extends V> after): 允许你在一个 Function 执行之后,再执行另一个 Function

  • compose(Function<? super V,? extends T> before): 允许你先执行一个 Function,然后再执行当前的 Function

    Function<Person, String> getName = Person::getName;
    Function<String, String> toUpperCase = String::toUpperCase;
    
    Function<Person, String> getNameInUpperCase = getName.andThen(toUpperCase);
    String name = getNameInUpperCase.apply(new Person("Alice", 25)); // 返回大写的名字
    
    andthen 是参数返回的结果string 再给下一个函数用
    compose 是拿到的参数person 先给前面的函数用 然后再给后面的函数用 所以这里需要<Person,Person>这种类型的lambda
    

Predicate 接口示例

接口的抽象方法

这里的Predicate检查Person对象的年龄是否超过20岁。

Predicate<Person> olderThan20=(p)->p.getAge()>20;
        boolean isOlderThan20=olderThan20.test(new Person("Alice",30));

接口的默认方法

Predicate 接口表示一个参数的布尔值函数。它包含了一些非常有用的默认方法,可以用来组合多个 Predicate 条件:

  • and(Predicate<? super T> other): 逻辑与。如果两个 Predicate 都为真,则结果为真。

  • or(Predicate<? super T> other): 逻辑或。如果任意一个 Predicate 为真,则结果为真。

  • negate(): 逻辑非。如果原 Predicate 为真,则结果为假,反之亦然。

    Predicate<Person> isAdult = p -> p.getAge() >= 18;
    Predicate<Person> isSenior = p -> p.getAge() >= 65;
    
    Predicate<Person> isAdultButNotSenior = isAdult.and(isSenior.negate());
    boolean test = isAdultButNotSenior.test(new Person("Alice", 25)); // 检查是否成年但不是老年人
    

接口的具体实现

(真看不明白直接用上面的就行)

Supplier供给型函数式接口:

public class supp {
    public static void main(String[] args) {
        //使用匿名内部类
        Supplier<Student> supplier = new Supplier<Student>() {
            @Override
            public Student get() {
                return new Student();
            }
        };
        Student student = supplier.get();
        student.hello();

        //使用lambda表达式
        Supplier<Student> supplier1 = () -> new Student();
        Student student1 = supplier1.get();
        student1.hello();

        //使用方法引用
        Supplier<Student> supplier2 = Student::new;
        Student student2 = supplier2.get();
        student2.hello();
    }
}


@FunctionalInterface   //函数式接口都会打上这样一个注解
interface Supplier<T> {
    T get();   //实现此方法,实现供给功能
}

class Student {
    public void hello() {
        System.out.println("我是学生!");
    }
}

Consumer消费型函数式接口:

public class cons {
    public static void main(String[] args) {
        //使用匿名内部类
        Consumer<Student2> consumer = new Consumer<Student2>() {
            @Override
            public void accept(Student2 student2) {
                student2.hello();
            }
        };
        consumer.accept(new Student2());

        //使用lambda表达式
        Consumer<Student2> consumer1 = (student2) -> student2.hello();
        consumer1.accept(new Student2());

        //使用方法引用 和 (str) -> str.toUpperCase();-> String::toUpperCase; 一个道理
//        一种语法糖 看多了就习惯了
        Consumer<Student2> consumer2 = Student2::hello;
        consumer2.accept(new Student2());

        //使用andThen方法
        Consumer<Student2> consumer3 = Student2::hello;//通过方法引用设定好一个customer
//        Consumer<Student2> consumer4 = new Consumer<Student2>() {
//            @Override
//            public void accept(Student2 student2) {
//                System.out.println("我是学生2!");
//            }
//        };
        Consumer<Student2> consumer4 = (student2) -> System.out.println("我是学生2!");
        Consumer<Student2> consumer5 = (student2) -> System.out.println("我是学生3!");
//      反正能一直then下去 先执行appcet然后是按顺序 andThen里面
        consumer3.andThen(consumer4).andThen(consumer5).accept(new Student2());
//        我是学生!
//        我是学生2!
//        我是学生3!
    }
}


@FunctionalInterface
interface Consumer<T> {
    void accept(T t);    //这个方法就是用于消费的,没有返回值

    default Consumer<T> andThen(Consumer<? super T> after) {   //这个方法便于我们连续使用此消费接口
        Objects.requireNonNull(after);
        return (T t) -> {
            accept(t);
            after.accept(t);
        };
    }
}

class Student2 {
    public void hello() {
        System.out.println("我是学生!");
    }
}

Function函数型函数式接口:

public class func {
    public static void main(String[] args) {
//        函数式接口的实现
        Function<Student3, String> function = Student3::hello;
        String apply = function.apply(new Student3());//student3

        //使用方法引用
        Function<Student3, String> function1 = Student3::hello;
        System.out.println(function1.apply(new Student3()));//student3

        //使用compose方法
        Function<Student3, String> function2 = Student3::hello;

        function2.compose(Student4::hello2).apply(new Student4());
//       f2的apply里的参数给compose用 compose用完还给apply
//       new Student4 给到了function2.compose(Student4::hello2)的参数 返回了一个新的new Student3
//       然后这个new Student3 再还给apply进行 (student3) -> student3.hello();

//
        //使用andThen方法
        Function<Student3, String> function4 = (student3) -> student3.hello();
        Function<String, String> function5 = (str) -> str.toUpperCase();
        System.out.println(function4.andThen(function5).apply(new Student3()));
//        先执行apply 拿到返回的str 再给到function5的apply方法


    }
}

@FunctionalInterface
interface Function<T, R> {
    R apply(T t);   //这里一共有两个类型参数,其中一个是接受的参数类型,还有一个是返回的结果类型

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

class Student3 {
    public String hello() {
        return "student3";
    }

    public Student3 H2() {
        return new Student3();
    }
}

class Student4 {
    public Student3 hello2() {
        return new Student3();
    }
}

Predicate断言型函数式接口:

public class pred {
    public static void main(String[] args) {
        //使用匿名内部类
        Predicate<Student5> predicate = new Predicate<Student5>() {
            @Override
            public boolean test(Student5 student5) {
                return student5.getScore() > 60;
            }
        };
        System.out.println(predicate.test(new Student5()));

        //使用lambda表达式
        Predicate<Student5> predicate1 = (student5) -> student5.getScore() > 60;
        System.out.println(predicate1.test(new Student5()));

        //使用and方法
        Predicate<Student5> predicate2 = (student5) -> student5.getScore() > 60;
        Predicate<Student5> predicate3 = (student5) -> student5.getScore() < 80;
        System.out.println(predicate2.and(predicate3).test(new Student5()));

        //使用or方法
        Predicate<Student5> predicate4 = (student5) -> student5.getScore() > 60;
        Predicate<Student5> predicate5 = (student5) -> student5.getScore() < 80;
        System.out.println(predicate4.or(predicate5).test(new Student5()));

        //使用negate方法
        Predicate<Student5> predicate6 = (student5) -> student5.getScore() > 60;
        System.out.println(predicate6.negate().test(new Student5()));

    }

}


@FunctionalInterface
interface Predicate<T> {
    boolean test(T t);    //这个方法就是我们要实现的

    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

class Student5 {
    public int score = 60;

    public int getScore() {
        return score;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值