简体   繁体   中英

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

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

If the two streams have the same order by user then you can zip them:

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).

If the two streams have different order you can use matchWith from rxjava2-extras .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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