Java8的新特性--函数式接口

时间:2023-03-09 19:22:48
Java8的新特性--函数式接口

函数式接口

Lambda表达式需要接口的支持,并且接口的抽象方法还只能有一个,要么没法区分实现的是哪一个抽象方法了。因此Lambda表达式需要函数式接口的支持

什么是函数式接口

接口中只有一个抽象方法的接口称为函数式接口。

函数式接口可以使用一个注解@FunctionalInterface修饰,此注解可以检查是否是函数式接口

函数式接口的使用

假设我们现在有一个需求:对一个数进行运算,什么运算都可以。如果我们想用Lambda表达式来实现的话,我们就需要一个接口来支持,下面我们先写一个MyFunction接口

@FunctionalInterface
public interface MyFunction {
public Integer getValue(Integer num);
}

然后我们需要一个运算的方法

    //运算方法
public Integer operation(Integer num, MyFunction fun){
return fun.getValue(num);
}

测试类

    @Test
public void test06(){
//平方运算
Integer num = operation(10,(x) -> x*x);
System.out.println(num);
//加和运算
Integer num2 = operation(100,(y) -> y+200);
System.out.println(num2); }

执行结果

100
300

不管是什么运算,我们只需要关注Lambda表达式的方法体如何实现运算就可以了。

通过以上例子发现,如果我们想用Lambda表达式来实现的话,我们就需要写一个接口来支持,是不是也挺麻烦的,其实,一般我们常用的接口,Java8里基本都已经给我们提供了,下面来看下Java8内置的核心接口。

Java8内置的四大核心函数式接口

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

我们先看一下Java8中给我定义好的Consumer接口

@FunctionalInterface
public interface Consumer<T> { /**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t); default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}

然后我们自己写个简单的例子看一下,写个开开森森去消费的方法

    @Test
public void test01(){
happy("一消费就很开森啦啦啦",str -> System.out.println(str));
}
public void happy(String str,Consumer<String> consumer){
consumer.accept(str);
}

运行结果

一消费就很开森啦啦啦

以上就是消费型接口,消费即是有去无回,有参数无返回值类型的接口。

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

我们先看一下Java8中给我定义好的Supplier接口

@FunctionalInterface
public interface Supplier<T> { /**
* Gets a result.
*
* @return a result
*/
T get();
}

我们再来写个例子,写个方法产生指定个数的整数并放入集合中

    @Test
public void test02(){
List<Integer> numList = getValue(10,() -> (int)(Math.random()*100));
for (Integer num :
numList) {
System.out.println(num);
}
} //产生指定个数的整数并放入集合中
public List<Integer> getValue(int num,Supplier<Integer> supplier){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add(supplier.get());
}
return list;
}

执行结果

84
22
60
29
52
34
9
95
34
6

以上就是供给型接口,只有产出,没有输入,即只有返回值没有入参,给你产生一些对象,并返回给你。

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

Java8中函数式接口的定义

@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); ····后面代码省略了····
}

下面写个处理字符串方法的例子

    @Test
public void test03(){
String returnStr = strHandler("\t\t\t我是一串有空格\t的字符串\t\t",(str) -> str.trim());
System.out.println(returnStr);
} public String strHandler(String str,Function<String,String> function){
return function.apply(str);
}

运行结果:trim()方法只是去除首尾的字符串

我是一串有空格	的字符串

以上就是一个函数型接口,一个参数,一个返回值,当然参数和返回值的类型可以相同

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

Java8中断言型接口的定义如下

@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);
····后面到吗省略····
}

下面我们写一个断言的例子,写个方法将满足条件的字符串放入集合中

    @Test
public void test04(){
List<String> list = Arrays.asList("aa","bbb","ccccc","dddddd");
//长度大于3的字符串放入集合中
List<String> reList = filterStr(list,(s) -> s.length() >3);
for (String str: reList) {
System.out.println(str);
}
} public List<String> filterStr(List<String> strList,Predicate<String> pre){
List<String> list = new ArrayList<>();
for (String str: strList) {
if(pre.test(str)){
list.add(str);
}
}
return list;
}

运行结果

ccccc
dddddd

以上是断言型接口,输入一个参数,输出一个boolean类型的返回值

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

除了上面四种类型的核心接口还有一些其他的不常用的接口,我们可以了解一下

  1. BiFunction<T, U, R>
  • 类型:参数类型T,U返回类型R
  • 用途:Function的子接口,对类型为T和U的参数应用操作,返回R类型的结果。包含有方法R apply(T t,U u)
  1. UnaryOperator(Function子接口)
  • 类型:参数类型T,返回类型T
  • 用途:Function的子接口,对类型为T的对象进行一元运算,并返回T类型的结果。包含有方法T apply(T t)
  1. BinaryOperator(BiFunction子接口)
  • 类型:参数类型T,T,返回类型T
  • 用途:BiFunction的子接口,对类型为T的对象进行二元运算,并返回T类型的结果。包含有方法T apply(T t1,T t2)
  1. BiConsumcr(T, U)
  • 类型:参数类型T, U,返回类型void
  • 用途:Consumcr的子接口,对类型为T,U的参数应用操作。包含有方法void accept(T t,U u)
  1. ToIntFunction、ToLongFunction、ToDoubleFunction
  • 类型:参数类型T,返回类型int long double
  • 用途:分别计算int long double值的函数
  1. IntFunction、LongFunction、DoubleFunction
  • 类型:参数类型int long double,返回类型T
  • 用途:参数分别为int long double类型的函数

总结

以上就是Java8内置的核心接口,我们在使用的过程中,按需所取就ok啦。