简体   繁体   中英

RxJava emit items with different periods

I have a Hashmap :

    private HashMap<Integer, ArrayList<EmitedItem>> contentHashMap;

Every EmitedItem is

public class EmitedItem {
public int duration;
public String title;}

And I must get first element from every ArrayList immediately, and then get the second after a duration of the previous. I'm not sure how to do this using RxJava

You can build a list of Observables for each item in the internal list and a delay applied with ever increasing amount:

Map<Integer, List<Item>> map = new HashMap<>();

map.put(1, Arrays.asList(Item.of(0, "A"), Item.of(1, "B"), Item.of(1, "C")));
map.put(2, Arrays.asList(Item.of(0, "D"), Item.of(1, "E")));

List<Observable<Item>> emitter = new ArrayList<>();

for (List<Item> list : map.values()) {
    int time = 0;

    for (Item i : list) {
        time += i.duration;
        emitter.add(Observable.just(i).delay(time, TimeUnit.SECONDS));
    }
}

Observable.merge(emitter).map(v -> v.title)
.toBlocking()
.subscribe(System.out::println);

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