一、ProjectReactor介绍
一、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);