zoukankan      html  css  js  c++  java
  • 【Java8新特性】还没搞懂函数式接口?赶快过来看看吧!

    写在前面

    Java8中内置了一些在开发中常用的函数式接口,极大的提高了我们的开发效率。那么,问题来了,你知道都有哪些函数式接口吗?

    函数式接口总览

    这里,我使用表格的形式来简单说明下Java8中提供的函数式接口。

    四大核心函数式接口

    首先,我们来看四大核心函数式接口,如下所示。

    函数式接口 参数类型 返回类型 使用场景
    Consumer消费型接口 T void 对类型为T的对象应用操作,接口定义的方法:void accept(T t)
    Supplier供给型接口 T 返回类型为T的对象,接口定义的方法:T get()
    Function<T, R>函数式接口 T R 对类型为T的对象应用操作,并R类型的返回结果。接口定义的方法:R apply(T t)
    Predicate断言型接口 T boolean 确定类型为T的对象是否满足约束条件,并返回boolean类型的数据。接口定义的方法:boolean test(T t)

    其他函数接口

    除了四大核心函数接口外,Java8还提供了一些其他的函数式接口。

    函数式接口 参数类型 返回类型 使用场景
    BiFunction(T, U, R) T, U R 对类型为T,U的参数应用操作,返回R类型的结果。接口定义的方法:R apply(T t, U u)
    UnaryOperator(Function子接口) T T 对类型为T的对象进行一 元运算, 并返回T类型的 结果。 包含方法为 T apply(T t)
    BinaryOperator (BiFunction 子接口) T, T T 对类型为T的对象进行二 元运算, 并返回T类型的 结果。 包含方法为 T apply(T t1, T t2)
    BiConsumer<T, U> T, U void 对类型为T, U 参数应用 操作。 包含方法为 void accept(T t, U u)
    ToIntFunction T int 计算int值的函数
    ToLongFunction T long 计算long值的函数
    ToDoubleFunction T double 计算double值的函数
    IntFunction int R 参数为int 类型的函数
    LongFunction long R 参数为 long类型的函数
    DoubleFunction double R 参数为double类型的函数

    四大核心函数式接口

    Consumer接口

    1.接口说明

    Consumer接口是消费性接口,无返回值。Java8中对Consumer的定义如下所示。

    @FunctionalInterface
    public 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); };
        }
    }
    

    2.使用示例

    public void handlerConsumer(Integer number, Consumer<Integer> consumer){
        consumer.accept(number);
    }
    
    @Test
    public void test1(){
        this.handlerConsumer(10000, (i) -> System.out.println(i));
    }
    

    Supplier接口

    1.接口说明

    Supplier接口是供给型接口,有返回值,Java8中对Supplier接口的定义如下所示。

    @FunctionalInterface
    public interface Supplier<T> {
        T get();
    }
    

    2.使用示例

    public List<Integer> getNumberList(int num, Supplier<Integer> supplier){
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < num; i++){
            list.add(supplier.get())
        }
        return list;
    }
    
    @Test
    public void test2(){
        List<Integer> numberList = this.getNumberList(10, () -> new Random().nextInt(100));
        numberList.stream().forEach(System.out::println);
    }
    

    Function接口

    1.接口说明

    Function接口是函数型接口,有返回值,Java8中对Function接口的定义如下所示。

    @FunctionalInterface
    public 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;
        }
    }
    

    2.使用示例

    public String handlerString(String str, Function<String, String> func){
        return func.apply(str);
    }
    
    @Test
    public void test3(){
        String str = this.handlerString("binghe", (s) -> s.toUpperCase());
        System.out.println(str);
    }
    

    Predicate接口

    1.接口说明

    Predicate接口是断言型接口,返回值类型为boolean,Java8中对Predicate接口的定义如下所示。

    @FunctionalInterface
    public 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);
        }
    }
    

    2.使用示例

    public List<String> filterString(List<String> list, Predicate<String> predicate){
        List<String> strList = new ArrayList<>();
        for(String str : list){
            if(predicate.test(str)){
                strList.add(str);
            }
        }
        return strList;
    }
    
    @Test
    public void test4(){
        List<String> list = Arrays.asList("Hello", "Lambda", "binghe", "lyz", "World");
        List<String> strList = this.filterString(list, (s) -> s.length() >= 5);
        strList.stream().forEach(System.out::println);
    }
    

    注意:只要我们学会了Java8中四大核心函数式接口的用法,其他函数式接口我们也就知道如何使用了!

    写在最后

    如果觉得文章对你有点帮助,请微信搜索并关注「 冰河技术 」微信公众号,跟冰河学习Java8新特性。

    最后,附上Java8新特性核心知识图,祝大家在学习Java8新特性时少走弯路。

  • 相关阅读:
    『Power AI by AI』 PAI-AutoML2.0重磅发布
    基于 K8s 做应用发布的工具那么多, 阿里为啥选择灰姑娘般的 Tekton ?
    编码方法论,赋能你我他
    开发部署效率提升 12 倍,这款应用托管服务让云上运维更简单
    全景还原报错现场 | 应用实时监控 ARMS 上线用户行为回溯功能
    告别诊断烦恼 | 应用实时监控 ARMS 上线智能和实时诊断功能
    阿里云CDN上线 WAF,一站式提供分发+安全能力
    RDS for PostgreSQL 云盘加密功能使用方法
    MySQL8.0.17
    2370 小机房的树
  • 原文地址:https://www.cnblogs.com/binghe001/p/12846649.html
Copyright © 2011-2022 走看看