Let's say we receive strings from 3 producers asynchronously. Once a certain amount of these objects have been received I want to iterate over them in an interleaved manner, that is, if receiving the following strings:
"a1" received from A,
"a2" received from A,
"c1" received from C,
"a3" received from A,
"b1" received from B,
"b2" received from B,
I'd like the "interleaved" iterator to return the strings as if we were iterating over the following list:
List<String> interleavedList = {"a1", "b1", "c1", "a2", "c2", "a3"},
So far I've created one List<String>
for each producer, and then I'm "iterating" over all the strings by working with the 3 list iterators (with a List<Iterator<String>>
). This works fine but I think there is a simpler way... Maybe by directly constructing the interleaved list while receiving the strings? but I don't see which Collection
or which Comparator
to use...
Note that I'm not so much interested in creating one list for each producer and then merging the 3 lists in a 4th interleaved list, as this will probably not be time-efficient.