I have two object lists both with a common attribute called timestamp and both are sorted by timestamp.
I want to broadcast these objects one by one based on the timestamp, example if first object of 1st list has timestamp < first object of 2nd list, broadcast first object of the 1st list and then compare second object of the 1st list with the first object of the 2nd list.
I am a newbie in java. This is what I have come up with:
//Merge the 2 object lists based on timestamp
ListIterator<x> xIterator = list1.listIterator();
ListIterator<y> yIterator = list2.listIterator();
while (xIterator.hasNext() && yIterator.hasNext()) {
if (xIterator.next().timestamp <= yIterator.next().timestamp) {
Bundle extra = new Bundle();
extra.putParcelable(LocalBroadcastConstants.ACTION, xIterator.previous());
MyBroadcastManager.getInstance(getTargetContext()).sendBroadcast(
new Intent(LocalBroadcastConstants.ACTION_SEND).putExtras(extra)
);
yIterator.previous();
} else {
Bundle extra = new Bundle();
extra.putParcelable(LocalBroadcastConstants.ACTION,
yIterator.previous());
MyBroadcastManager.getInstance(getTargetContext()).sendBroadcast(
new Intent(LocalBroadcastConstants.ACTION_SEND).putExtras(extra)
);
xIterator.previous();
}
I know my logic is incorrect because for the first item in the Iterators, xIterator.previous and yIterator.previous will point to nothing. I can't seem to find the correct solution for this problem statement. Please help.