JDK8--04:内置接口

在JDK8--3中已经说过,使用lambda方法需要新增函数式接口,为了使用方便,JDK8已经提供了许多内置接口,总的归纳来说,有四大函数式接口。

/**
     *
     * java8 四大内置接口
     *
     * 1、消费性接口:Consumer<T>
     *     默认方法:void accept(T t)
     *
     * 2、供给型接口:Supplier<T>
     *     默认方法:T get()
     *
     * 3、函数型接口:Function<T, R>
     *     默认方法:R apply(T t)
     *
     * 4、断言型接口:Predicate<T>
     *     默认方法:boolean test(T t)
     */

1、消费型接口,Consumer<T>,默认方法:void accept(T t)

使用场景:传入一个参数,在方法内对参数做处理,不做返回。

demo需求:对一个double入参乘以5之后做输出

demo示例:

public void consumerMethod(double d, Consumer consumer){
        consumer.accept(d);
    }

    @Test
    public void consumerTest(){
        log.info("consumerTest输出============={}");
        consumerMethod(100, m -> log.info("{}*5={}", m, (double)m*5));
    }

测试结果:

 2、供给型接口,Supplier<T>,默认方法:T get()

 使用场景:无入参,有返回

 demo场景:返回一个100以内随机整数的集合

 demo样例:

public List supplierMethod(int num, Supplier supplier){
        List list = new ArrayList<>();
        for (int i=0;i< num;i++){
            list.add(supplier.get());
        }
        return list;
    }

    @Test
    public void supplierTest(){
        List<Integer> s = supplierMethod(5,()-> (int)(Math.random()*100));
        log.info("supplierTest输出============={}",JSON.toJSONString(s));
    }

测试结果:

 3、函数型接口:Function<T, R>, 默认方法:R apply(T t)

使用场景:有入参,有出参

demo需求:对一个字符串进行切割,返回切割后的第一个字符串

demo样例:

public String functionMethod(String num, Function<String,String> function){
return function.apply(num);
}

@Test
public void functionTest(){
String s = functionMethod("354sd654sdasd",(x)->x.split("s")[0]);
log.info("functionTest输出============={}",s);
}

demo输出:

 4、断言型接口:Predicate<T>,默认方法:boolean test(T t)

使用场景:有入参,按照条件判断,返回boolean

demo需求:对一个字符串集合做判断,如果不包含指定字符串,就从集合中剔除

demo样例:

    public List<String> preticateMethod (List<String> list, Predicate<String> predicate){
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            if(!predicate.test((String) iterator.next())){
                iterator.remove();
            }
        }
        return list;
    }

    @Test
    public void preticateTest(){
        List<String> list = new ArrayList<>();
        list.add("654564");
        list.add("874984");
        list.add("adsfdf");
        list.add("asfd");
        list.add("ghgfhgfh");
        list.add("54ads");
        log.info("functionTest输出============={}", JSON.toJSONString(preticateMethod(list,(x)->x.contains("a"))));
    }

  demo测试结果:

5、其实,上述4个内置接口是我们平时使用的比较多的内置接口,除了这四个接口外,JDK8还提供其他的子类以供使用

  以Function接口为例,需求可能不止一个入参,可能是多个入参,有的需求可能是我的入参指定必须是double,或者出参必须是duoble,针对不同的需求,JDK8都做了对应的内置函数,例如

* Function 的子接口
* BiFunction<T, U, R> 
    R apply(T t, U u);
* UnaryOperator<T> extends Function<T, T> * BinaryOperator<T> extends BiFunction<T,T,T> * ToDoubleFunction<T>   
    double applyAsDouble(T value); * ToDoubleBiFunction<T, U>
    double applyAsDouble(T t, U u); * ToIntBiFunction<T, U>
    int applyAsInt(T t, U u); * ToIntFunction<T>
    int applyAsInt(T value); * ToLongBiFunction<T, U>
    long applyAsLong(T t, U u); * ToLongFunction<T>
    long applyAsLong(T value); * LongFunction<R>
    R apply(long value);

  上述只是举例说明JDK8针对不同的需求有不同的内置接口,并非全量的接口。

原文地址:https://www.cnblogs.com/liconglong/p/12973398.html