JDK8之四大核心函数式接口
四大核心函数式接口Consumer 接口接口说明Consumer 接口是消费性接口无返回值。Java8 中对 Consumer 的定义如下所示。/** * Represents an operation that accepts a single input argument and returns no * result. Unlike most other functional interfaces, {code Consumer} is expected * to operate via side-effects. * * pThis is a a hrefpackage-summary.htmlfunctional interface/a * whose functional method is {link #accept(Object)}. * * param T the type of the input to the operation * * since 1.8 */FunctionalInterfacepublicinterfaceConsumerT{/** * Performs this operation on the given argument. * * param t the input argument */voidaccept(Tt);/** * Returns a composed {code Consumer} that performs, in sequence, this * operation followed by the {code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {code after} operation will not be performed. * * param after the operation to perform after this operation * return a composed {code Consumer} that performs in sequence this * operation followed by the {code after} operation * throws NullPointerException if {code after} is null */defaultConsumerTandThen(Consumer?superTafter){Objects.requireNonNull(after);return(Tt)-{accept(t);after.accept(t);};}使用说明publicvoidhandlerConsumer(Integernumber,ConsumerIntegerconsumer){consumer.accept(number);}Testpublicvoidtest1(){this.handlerConsumer(10000,(i)-System.out.println(i));}Supplier 接口接口说明Supplier 接口是供给型接口有返回值Java8 中对 Supplier 接口的定义如下所示。FunctionalInterfacepublicinterfaceSupplierT{Tget();}使用示例publicListIntegergetNumberList(intnum,SupplierIntegersupplier){ListIntegerlistnewArrayList();for(inti0;inum;i){list.add(supplier.get())}returnlist;}Testpublicvoidtest2(){ListIntegernumberListthis.getNumberList(10,()-newRandom().nextInt(100));numberList.stream().forEach(System.out::println);}Function 接口接口说明Function 接口是函数型接口有返回值Java8 中对 Function 接口的定义如下所示。FunctionalInterfacepublicinterfaceFunctionT,R{Rapply(Tt);defaultVFunctionV,Rcompose(Function?superV,?extendsTbefore){Objects.requireNonNull(before);return(Vv)-apply(before.apply(v));}defaultVFunctionT,VandThen(Function?superR,?extendsVafter){Objects.requireNonNull(after);return(Tt)-after.apply(apply(t));}staticTFunctionT,Tidentity(){returnt-t;}}使用示例publicStringhandlerString(Stringstr,FunctionString,Stringfunc){returnfunc.apply(str);}Testpublicvoidtest3(){Stringstrthis.handlerString(binghe,(s)-s.toUpperCase());System.out.println(str);}Predicate 接口接口说明Predicate 接口是断言型接口返回值类型为 booleanJava8 中对 Predicate 接口的定义如下所示。FunctionalInterfacepublicinterfacePredicateT{booleantest(Tt);defaultPredicateTand(Predicate?superTother){Objects.requireNonNull(other);return(t)-test(t)other.test(t);}defaultPredicateTnegate(){return(t)-!test(t);}defaultPredicateTor(Predicate?superTother){Objects.requireNonNull(other);return(t)-test(t)||other.test(t);}staticTPredicateTisEqual(ObjecttargetRef){return(nulltargetRef)?Objects::isNull:object-targetRef.equals(object);}}使用示例publicListStringfilterString(ListStringlist,PredicateStringpredicate){ListStringstrListnewArrayList();for(Stringstr:list){if(predicate.test(str)){strList.add(str);}}returnstrList;}Testpublicvoidtest4(){ListStringlistArrays.asList(Hello,Lambda,binghe,lyz,World);ListStringstrListthis.filterString(list,(s)-s.length()5);strList.stream().forEach(System.out::println);}