文章0 | 阅读 9582 | 点赞0
处理业务的时候一定有这样的需求:将多个源数据压缩成一个,Reactor提供了zip和zipWith方法可以做到这一点。
zip和zipwith有些不同:
private Flux<String> name () {
return Flux.just("ffzs", "dz", "sleepycat");
}
private Flux<Integer> age () {
return Flux.just(12, 22, 32);
}
private Flux<Integer> salary () {
return Flux.just(10000, 20000, 30000);
}
@Data
@AllArgsConstructor
static class Employee {
String name;
Integer age;
Integer salary;
}
@Data
@AllArgsConstructor
static class User {
String name;
Integer age;
}
多个源压缩到一起,等待所有源发出一个元素之后,将这些元素进行组合
图示:
@Test
public void zipTest () {
Flux<Tuple3<String, Integer, Integer>> flux = Flux.zip(name(), age(), salary());
flux.subscribe(i -> log.info(i.toString()));
}
@Test
public void zipTest () {
Flux<Tuple3<String, Integer, Integer>> flux = Flux.zip(name(), age(), salary());
Flux<Employee> employee = flux.map(tuple -> new Employee(tuple.getT1(), tuple.getT2(), tuple.getT3()));
employee.subscribe(i -> log.info(i.toString()));
}
@Test
public void zipCombineTest () {
Flux<Employee> flux = Flux.zip(objects -> {
return new Employee((String)objects[0], (Integer)objects[1], (Integer)objects[2]);
}, name(), age(), salary());
flux.subscribe(i -> log.info(i.toString()));
}
图示:
跟with方法差不多,只能处理两个源:
@Test
public void zipWithTest () {
Flux<User> flux = name().zipWith(age(), (name, age) -> new User(name, age));
flux.subscribe(i -> log.info(i.toString()));
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://fanfanzhisu.blog.csdn.net/article/details/107891969
内容来源于网络,如有侵权,请联系作者删除!