浅析Java8新特性-四大内置核心函数式接口 :消费型Consumer(void accept)、供给型Supplier(T get)、函数型Funciton(R apply)、断言型Predicate(boolean test)

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了浅析Java8新特性-四大内置核心函数式接口 :消费型Consumer(void accept)、供给型Supplier(T get)、函数型Funciton(R apply)、断言型Predicate(boolean test)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_360_1@  在前面一篇文章简单介绍了Lambda表达式,我们知道使用Lambda表达式是需要使用函数式接口的,那么岂不是在开发过程中需要定义许多函数式接口?当时我就有这个疑问,每次还需要自定义一个纯函数式接口,也挺烦的。

  后来了解到其实不然,Java8 其实已经为我们定义好了4类内置函数式接口,这4类接口已经可以解决我们开发过程中绝大部分的问题,只有一小部分比较特殊的情况需要我们自己去定义函数式接口,本文就简单来学习一下Java8内置的4大核心函数式接口。

一、Consumer<T>:消费型接口(void accept(T t))

@FunctionalInterface
public interface Consumer<T> {
    /**
     * PErforms this operation on the given argument.
     * @param t the input argument
     */
    void accept(T t);
}
// 消费型接口Consumer<T>
@test
public void test1 () {
    consumo(500, (x) -> System.out.PRintln(x));
}
public void consumo (double money, Consumer<Double> c) {
  c.accept(money);
}

  这是一个简单的例子,我们需要了解的就是我们把 lambda 表达式 (x) -> System.out.println(x)&nbsp; 当做一个参数(参数为函数),传递给了 方法 consumo,方法 consumo 里是用 消费型接口 Consumer 接参的,然后就可以直接调用 消费型接口 的 accept 方法。

  以上为消费型接口,有参数,无返回值类型的接口。

二、Supplier<T>:供给型接口(T get())

@FunctionalInterface
public interface Supplier<T> {
    /**
     * Gets a result.
     * @return a result
     */
    T get();
}
    // 供给型接口,Supplier<T>
    @Test
    public void test2 () {
        Random ran = new Random();
        List<Integer> list = supplier(10, () -> ran.nextInt(10));
        for (Integer i : list) {
            System.out.println(i);
        }
    }
    /**
     * 随机产生sum个数量得集合
     * @param sum 集合内元素个数
     * @param sup
     * @return
     */
    public List<Integer> supplier(int sum, Supplier<Integer> sup){
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < sum; i++) {
            list.add(sup.get());
        }
        return list;
    }

  具体同上面分析咯,这就是一个供给类型的接口,只有产出,没有输入,就是只有返回值,没有入参。

三、Function<T, R>:函数型接口(R apply(T t))

@FunctionalInterface
public interface Function<T, R> {
    /**
     * Applies this function to the given argument.
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
}
import java.util.function.Function;
public class Test2 {
    // 函数型接口:Function<R, T>
    public static void main(String[] args) {
        String newStr = strHandler("abc",(str)->str.toUpperCase());
        System.out.println(newStr);
        newStr = strHandler("  abc  ",(str)->str.trim());
        System.out.println(newStr);
    }
    /**
     * 字符串操作
     * @param str 需要处理得字符串
     * @param fun Function接口
     * @return 处理之后得字符传
     */
    public static String strHandler(String str, Function<String,String> fun){
        return fun.apply(str);
    }
}

  我们可以看到第一个输出:ABC,第二个输出:abc。这就是一个函数型接口,输入一个类型得参数,输出一个类型得参数,当然两种类型可以一致。

四、Predicate<T>:断言型接口(boolean test(T t))

@FunctionalInterface
public interface Predicate<T> {
    /**
     * Evaluates this predicate on the given argument.
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}
import java.util.function.Predicate;
public class Test2 {
    // 断言型接口:Predicate<T>
    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello","jiangshuying","lambda","www","ok","q");
        List<String> ret = filterstr(list,(str)->str.length()>2);
        System.out.println(ret);
    }
    /**
     * 过滤集合
     * @param list
     * @param pre
     * @return
     */
    public static List<String> filterStr(List<String> list, Predicate<String> pre){
        ArrayList<String> arrayList = new ArrayList<>();
        for(String str:list){
            if(pre.test(str)) {
                arrayList.add(str);
            }
        }
        return arrayList;
    }
}

  上面例子判断一些字符串数组判断长度>2的字符串,这就是一个断言型接口,输入一个参数,输出一个boolean类型得返回值。

五、其他类型的一些函数式接口

  除了上述得4种类型得接口外还有其他的一些接口供我们使用:

1、.BiFunction<T, U, R> :参数类型有2个,为T,U,返回值为R,其中方法为R apply(T t, U u)

2、.UnaryOperator<T>(Function子接口):参数为T,对参数为T的对象进行一元操作,并返回T类型结果,其中方法为T apply(T t)

3、.BinaryOperator<T>(BiFunction子接口):参数为T,对参数为T得对象进行二元操作,并返回T类型得结果,其中方法为T apply(T t1, T t2)

4、.BiConsumcr(T, U) :参数为T,U无返回值,其中方法为 void accept(T t, U u)

5、.ToIntFunction<T>、ToLongFunction<T>、ToDoubleFunction<T>:参数类型为T,返回值分别为int,long,double,分别计算int,long,double得函数。

6、.IntFunction<R>、LongFunction<R>、DoubleFunction<R>:参数分别为int,long,double,返回值为R。

  以上就是Java8内置得核心函数式接口,其中包括了大部分得方法类型,所以可以在使用得时候根据不同得使用场景去选择不同得接口使用。

脚本宝典总结

以上是脚本宝典为你收集整理的浅析Java8新特性-四大内置核心函数式接口 :消费型Consumer(void accept)、供给型Supplier(T get)、函数型Funciton(R apply)、断言型Predicate(boolean test)全部内容,希望文章能够帮你解决浅析Java8新特性-四大内置核心函数式接口 :消费型Consumer(void accept)、供给型Supplier(T get)、函数型Funciton(R apply)、断言型Predicate(boolean test)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。