简体   繁体   English

如何在Rxjava中通过键合并两个Observable?

[英]how to merge two Observable by key in Rxjava?

I have a Observable a 我有一个可观察的

class User {
    public int userId;
    public String userName;
    public int age;
    public Boolean vip;
}

Dataset: 数据集:

userId  userName  age   vip
   1       ham     21  false
   2       lily    18  false
   3       potter  38  false

Observable b 可见b

class VIP {
    public int userId;
    public Boolean vip;
}

Dataset: 数据集:

userId  vip
   1   true

the expected merge result: 预期的合并结果:

userId  userName  age   vip
   1       ham     21  true
   2       lily    18  false
   3       potter  38  false

As known, Rxjava has Merge , Concat , Zip , Join , but they all seems like can't do this 众所周知,Rxjava具有MergeConcatZipJoin ,但是它们似乎都无法做到这一点

If the two streams have the same order by user then you can zip them: 如果两个流的用户顺序相同,则可以zip它们:

users.zipWith(vips, (u,v) -> new User(u.userName, u.userId, u.age, v.vip))

You could modify the u but best to prefer immutability (so create a new object). 您可以修改u但最好选择不变性(因此创建一个新对象)。

If the two streams have different order you can use matchWith from rxjava2-extras . 如果两个流具有不同的顺序,你可以使用matchWithrxjava2-演员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM