<span role="heading" aria-level="2">Java8新特性之函数式接口</span>

Java8中提供了许多新特性,其中之一就是函数是编程,官方提供了一些常用的函数式接口,基本已经满足日常使用,简单介绍一下常用的一些函数式接口。

消费型接口Consumer<T>

/**
 * 消费型接口 void accept(T t);
 * @param count
 * @param consumer
 */
public static void consumer(int count, Consumer<Integer> consumer) {
    consumer.accept(count);
}

调用:

consumer(100, (count) -> System.out.println("Consumer Type Interface: " + count));

输出结果:

Consumer Type Interface: 100

供给型接口Supplier<T>

/**
 * 供给型接口 T get();
 * 获取一组随机数
 * @return
 */
public static List<Integer> getRandoms(int count, Supplier<Integer> supplier) {
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        Integer integer = supplier.get();
        list.add(integer);
    }
    return list;
}

调用:

List<Integer> randoms = getRandoms(5, () -> (int) (Math.random() * 100));
System.out.println("Supplier Type Interface: "+randoms);

输出结果:

Supplier Type Interface: [80, 69, 94, 44, 2]

断言型接口Predicate<T>

/**
 * 断言型接口  boolean test(T t);
 * 字符串集合过滤
 * @param stringList
 * @param predicate
 * @return
 */
public static List<String> filterString(List<String> stringList, Predicate<String> predicate) {
    List<String> list = new ArrayList<>();
    stringList.forEach(str -> {
        if (predicate.test(str)) {
            list.add(str);
        }
    });
    return list;
}

调用:

List<String> list = filterString(Arrays.asList("Hello","World","!"), (s) -> s.length() >= 5);
System.out.println("Predicate Type Interface: "+list);

输出结果:

Predicate Type Interface: [Hello, World]

函数型接口Function<T, R>

/**
 * 函数型接口 R apply(T t);
 * @param string
 * @param function
 * @return
 */
public static String handleString(String string, Function<String, String> function){
     return function.apply(string);
}

调用:

String s = handleString("Hello zs!", (string) -> string.replace("zs", "world"));
System.out.println("Function Type Interface: "+s);

输出结果:

Function Type Interface: Hello world!

上面简单的例子也说明了,使用这些内置函数式接口方便我们写一些通用型工具,比如字符串操作的通用方法等等,大大提高代码的通用性,提高我们的开发效率,这里只是列举了一些简单的使用案例,上述接口中还有其他的方法,需要我们在实际使用中慢慢发掘。

常用接口

下面列举Java8中内置的函数式接口:

接口名称 描述
BiConsumer<T,U> 表示了一个接收两个输入参数的操作,并且不返回任何结果
BiFunction<T,U,R> 表示了一个接收两个输入参数的方法,并且返回一个结果
BinaryOperator 表示了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果
BiPredicate<T,U> 表示了一个两个参数的boolean值方法
BooleanSupplier 表示了boolean值结果的提供方
DoubleBinaryOperator 表示了作用于两个double值操作符的操作,并且返回了一个double值的结果。
DoubleConsumer 表示一个接收double值参数的操作,并且不返回结果。
DoubleFunction 表示接收一个double值参数的方法,并且返回结果
DoublePredicate 表示一个拥有double值参数的boolean值方法
DoubleSupplier 表示一个double值结构的提供方
DoubleToIntFunction 接收一个double类型输入,返回一个int类型结果。
DoubleToLongFunction 接收一个double类型输入,返回一个long类型结果
DoubleUnaryOperator 接收一个参数同为类型double,返回值类型也为double 。
IntBinaryOperator 接收两个参数同为类型int,返回值类型也为int 。
IntConsumer 接收一个int类型的输入参数,无返回值 。
IntFunction 接收一个int类型输入参数,返回一个结果 。
IntPredicate 接收一个int输入参数,返回一个布尔值的结果。
IntSupplier 无参数,返回一个int类型结果。
IntToDoubleFunction 接收一个int类型输入,返回一个double类型结果 。
IntToLongFunction 接收一个int类型输入,返回一个long类型结果。
IntUnaryOperator 接收一个参数同为类型int,返回值类型也为int 。
LongBinaryOperator 接收两个参数同为类型long,返回值类型也为long。
LongConsumer 接收一个long类型的输入参数,无返回值。
LongFunction 接收一个long类型输入参数,返回一个结果。
LongPredicateR 接收一个long输入参数,返回一个布尔值类型结果。
LongSupplier 无参数,返回一个结果long类型的值。
LongToDoubleFunction 接收一个long类型输入,返回一个double类型结果。
LongToIntFunction 接收一个long类型输入,返回一个int类型结果。
LongUnaryOperator 接收一个参数同为类型long,返回值类型也为long。
ObjDoubleConsumer 接收一个object类型和一个double类型的输入参数,无返回值。
ObjIntConsumer 接收一个object类型和一个int类型的输入参数,无返回值。
ObjLongConsumer 接收一个object类型和一个long类型的输入参数,无返回值。
ToDoubleBiFunction<T,U> 接收两个输入参数,返回一个double类型结果
ToDoubleFunction 接收一个输入参数,返回一个double类型结果
ToIntBiFunction<T,U> 接收两个输入参数,返回一个int类型结果。
ToIntFunction 接收一个输入参数,返回一个int类型结果。
ToLongBiFunction<T,U> 接收两个输入参数,返回一个long类型结果。
ToLongFunction 接收一个输入参数,返回一个long类型结果。
UnaryOperator 接收一个参数为类型T,返回值类型也为T。

这些接口位于java.util包中,这也能看出这些内置接口的作用定位,帮助我们高效的开发。

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务