设为首页 加入收藏

TOP

JAVA函数式编程(三)
2023-08-06 07:49:34 】 浏览:88
Tags:JAVA
erExample { public static void main(String[] args) { Supplier<String> messageSupplier = () -> "Hello, World!"; String message = messageSupplier.get(); System.out.println(message); // 输出:Hello, World! } }

在上面的示例中,我们使用了 Supplier<String> 函数式接口创建了一个供给型对象 messageSupplier,它返回一个固定的字符串 "Hello, World!"。然后通过调用 messageSupplier.get() 方法获取供给型对象的结果。

理解Consumer<T>

它代表了一个消费型接口。Consumer 接口接受一个参数,但没有返回值。该接口有一个抽象方法 void accept(T t),用于执行接受到的参数的操作。Consumer 接口通常用于对某个对象或值进行处理,例如打印输出、写入文件、发送消息等操作。由于没有返回值,Consumer 接口更多地用于执行一些操作,而不是产生一个结果。数据是只进不出,一般是作为方法参数或者构造器

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * 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
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

案例

public class C303_Consumer {
    public static void main(String[] args) {
        Consumer consumer = System.out::println;
        Consumer<String> consumer2 = C303_Consumer::println;
        // 轮流消费
        consumer2.andThen(consumer).andThen(consumer2).andThen(consumer2).accept("hello world2");
        consumer.accept("hello world1");
    }

    public static void println(String message){
        System.out.println("println:"+message);
    }
}

打印结果:

println:hello world2
hello world2
println:hello world2
println:hello world2
hello world1

理解Function<T,R>

数据转换类型,是一个函数式接口,它代表了一个函数,该函数接受一个参数类型为 T 的输入,并返回一个参数类型为 R 的结果。Function 接口有一个抽象方法 R apply(T t),用于执行函数的逻辑,将输入类型 T 转换为输出类型 R。

Function 接口常用于对一个对象或值进行转换、映射或计算,并返回一个结果。它将一个类型的数据映射到另一个类型,常用于对集合的处理、数据转换等场景。

示例使用 Function 接口:

public class C304_Function {
   public static void main(String[] args) {
      /**示例一:将入参为String转为Integer类型返回*/
      Function <String,Integer> function1 = new Function<String, Integer>() {
         @Override
         public Integer apply(String s) {
             System.out.println("function1");
            return Integer.parseInt(s);
         }

      };
      /**通过Function里面的apply函数调用*/
      // System.out.println(function1.apply("123"));
       /**示例二:将入参为Integer转为String类型返回*/
       Function<Integer,String> function2 = new Function<Integer, String>() {
           @Override
           public String apply(Integer integer) {
               System.out.println("function2");
               return String.valueOf(integer);
           }
       };
       /**示例三:将入参为String转为String类型返回*/
       Function<String,String> function3 = new Function<String, String>() {
           @Override
           public String apply(String string) {
               System.out.println("function3");
               return String.valueOf(string);
           }
       };
       // 执行循序:fun2 -> fun3 -> fun1
       Integer apply = function1.compose(function3).compose(function2).apply(
首页 上一页 1 2 3 4 5 6 下一页 尾页 3/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇@ControllerAdvice 注解使用及原.. 下一篇《深入理解Java虚拟机》读书笔记..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目