ProjectReactor反应式编程基础

本文介绍了ProjectReactor中的Flux和Mono概念,展示了如何创建0-1和0-n结果的流,以及编程实战中如何使用flatMap、distinct、filter等操作处理字符串。
摘要由CSDN通过智能技术生成

一、ProjectReactor介绍

官网Reactor 3 Reference Guide

一、Flux和Mono

Flux :an asynchronous 0-n result

Mono: an asynchronous 0-1 result

三、Flux/Mono创建流的方式

        //Mono创建0-1个序列
        Mono.empty().subscribe(System.out::println);
        Mono.just("hello").subscribe(System.out::println);

        //Flux创建0-n个序列
        Flux.just(1,2,3,4,5,6).subscribe(i->System.out.printf("%d\t",i));
        Flux.fromIterable(Arrays.asList('A','B','C','D','E','F')).subscribe(System.out::println);
        Flux.fromArray(new String[]{"A","B","C","D","E","F"}).subscribe(i->System.out.printf("%s\t",i));
        Flux.fromStream(Stream.of(1,2,3,4,5,6,7)).subscribe(System.out::println);
        Flux.range(1,100).subscribe(System.out::println);

        //打印99乘法表
        Flux.generate(()->1,(p,sink)->{
            StringBuilder builder = new StringBuilder();
            for(int i=11;i<=p;i++){
                builder.append(i).append("*").append(p).append("=").append(i*p).append("\t\t");
            }
            sink.next(builder.toString());
            if(p==19){
                sink.complete();
            }
            return p+1;
        }).subscribe(System.out::println);

四、编程实战

        /**
         * 输入 : hello world,this is my world,welcome to my world.
         * 输出 :
         */

        String src = "hello world,this is my world,welcome to my world.";
        Flux.fromArray(src.split(" "))
                .flatMap(s -> Flux.fromArray(s.split("")))
                .distinct()
                .filter(s -> s.matches("[a-zA-Z]"))
                .sort()
                .subscribe(System.out::println);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值