美文网首页
新特性 函数式接口

新特性 函数式接口

作者: 测试员 | 来源:发表于2019-08-19 13:00 被阅读0次

概述

有且只有一个抽象方法的接口,称之为函数式接口,当然接口中可以包含其它的方法(默认,静态,私有)

定义一个函数式接口

    /**
    * @author Yuan-9826
    * @FunctionalInterface 检验是否是函数式接口的标签
    */
    
    @FunctionalInterface
    public interface FuncDemo1 {
        /**
        * 定义一个抽象方法
        * 这是一个Demo
        */
        public abstract void method();
    }

常用接口

1.接口:supplier<T>【是个生产型接口】

接口方法:T get()

例:

    class LambdaDemo1 {
        public static void main(String[] args) {
            String a = "a";
            String b = "b";
            System.out.println(getString(()-> a+b));
        }
        public static String getString(Supplier<String> function){
            return function.get();
        }
    }

2.接口:Consumer<T>【是个生产型接口】

接口方法:void accept(T t)【抽象方法】

例:

    class LambdaDemo3 {
        public static void main(String[] args) {
            name("GoWin",(String name)->{
                //反转字符串
                System.out.println(new StringBuffer(name).reverse().toString());
                    }
    
            );
        }
    
        public static void name(String name, Consumer<String> str) {
            str.accept(name);
        }
    }
接口方法: andThen【默认方法】

例1:

    class LambdaDemo4 {
        public static void main(String[] args) {
            String str = "AaBbCcDd";
            method(str,
                    (m) -> System.out.println(str.toUpperCase()),
                    (m) -> System.out.println(str.toLowerCase())
                );
    
        }
    
        public static void method(String str, Consumer<String> c1, Consumer<String> c2) {
            c1.andThen(c2).accept(str);
        }
    }

例2:

    class LambdaDemo5 {
        public static void main(String[] args) {
            String[] arr = {"男,张三", "女,李四", "男,王五", "女,赵六,"};
            method(arr, (r) -> {
                System.out.print(r.split(",")[0] + "==");
            }, (r) -> {
                System.out.println(r.split(",")[1]);
            });
        }
    
        public static void method(String[] arr, Consumer<String> c1, Consumer<String> c2) {
            for (String s : arr) {
                c1.andThen(c2).accept(s);
            }
        }
    }

3.接口: Predicate

接口方法:boolean test(T t)【抽象方法】

例:

    class LambdaDemo7 {
        public static void main(String[] args) {
            String a = "abcdefghijklmn";
    //        只有一个参数不用写类型
    //        返回值很简单不用写return
            System.out.println(isLength(a,pre->pre.length()==5));
        }
    
        public static boolean isLength(String str, Predicate<String> pre) {
            return  pre.test(str);
        }
    }
接口方法:boolean and(T t)【默认方法】

例:

    class LambdaDemo8 {//满足所有条件
        public static void main(String[] args) {
            String a = "asdfzxcvqwer";
    
            boolean b = checkLL(a
                    , (str) -> {
                        return str.length() > 1;
                    }
                    , (str) -> {
                        return str.contains("g");
                    }
            );
            System.out.println(b);
        }
    
        private static boolean checkLL(String str, Predicate<String> pre1, Predicate<String> pre2) {
            return pre1.and(pre2).test(str);
    
        }
    }
接口方法:boolean or(T t)【默认方法】

例:

    class LambdaDemo9 {//满足任意条件
        public static void main(String[] args) {
            String a = "asdfzxcvqwer";
    
            boolean b = checkLL(a
                    , (str) -> {
                        return str.length() > 1;
                    }
                    , (str) -> {
                        return str.contains("g");
                    }
            );
            System.out.println(b);
        }
    
        private static boolean checkLL(String str, Predicate<String> pre1, Predicate<String> pre2) {
            return pre1.or(pre2).test(str);
    
        }
    }
接口方法:boolean negate(T t)【默认方法】

例:

    class LambdaDemo10 {
        public static void main(String[] args) {
            //取反
            String like = "like";
            System.out.println(checkLL(like,pre->pre.length()>1));
    
        }
        private static boolean checkLL(String str, Predicate<String> pre){
            return pre.negate().test(str);
        }
    }

3.接口: Function<T,R>

接口方法: R apply(T t)【抽象方法】

例:

    class LambdaDemo11 {
        public static void main(String[] args) {
            String a = "654123";
            Integer ch = change(a, fun -> Integer.parseInt(a));
            System.out.println("ch = " + ch);
        }
    
        public static Integer change(String str,Function<String,Integer> func){
            return func.apply(str);
        }
    }
接口方法: andThen【默认方法】

例:

    class LambdaDemo12 {
        public static void main(String[] args) {
            String s = "13206";
            Integer c = change(s, fuc -> Integer.parseInt(s), fuc -> fuc - 900);
            System.out.println("c = " + c);
        }
    
        public static Integer change(String str,Function<String,Integer> func1,Function<Integer,Integer> func2){
            return func1.andThen(func2).apply(str);
        }
    }

相关文章

  • @FunctionalInterface函数式接口

    JDK8新特性:函数式接口@FunctionalInterface的使用说明

  • JAVA8函数式接口学习

    JAVA8函数式接口 函数式接口是java8的一种新特性,函数式接口定义了且只定义了一个抽象方法!该接口非常有用,...

  • 函数式接口

    函数式接口 声明:java8新特性系列为个人学习笔记,参考地址点击这里,侵删!! 函数式接口(Functional...

  • 新特性 函数式接口

    概述 有且只有一个抽象方法的接口,称之为函数式接口,当然接口中可以包含其它的方法(默认,静态,私有) 定义一个函数...

  • JAVA8新特性之函数式接口使用与学习

    函数式接口 函数式接口是Java8引用的一个新特性,是一种特殊的接口 SAM类型的接口(Single Abstra...

  • FunctionalInterface函数式接口

    关于jdk8的新特性函数式接口示例以及描述 代码示例

  • JDK新特性(四)——函数式接口

    前言 函数式接口是JDK1.8推出的新特性之一,可以说函数式接口给Lambda表达式这种函数提供了简便的使用环境,...

  • Lambda表达式

    Lambda表达式是Java8的一个新特性,是函数式接口的一种体现。所谓函数式接口(functional inte...

  • JDK1.8新特性

    JDK1.8新特性 1. 函数式接口 含义:有且仅有一个抽象方法,但可以有多个非抽象方法的接口 函数式接口,就是J...

  • Java 8 新特性 - 函数式接口 Functional In

    Java8的其中一个新特性,函数式接口。 什么是函数式接口?有且仅有一个抽象方法的接口(不包括默认方法、静态方法以...

网友评论

      本文标题:新特性 函数式接口

      本文链接:https://www.haomeiwen.com/subject/wkessctx.html