Comparing consecutive elements in a queue
I have a queue of elements, sorted by date. I need to extract the first n
elements, which have the same date and add them to a temporary ArrayList,
from which I choose one of them and scrap the others. After that I need to
continue doing the same thing for the next n elements of the queue with
the same date (extract them to the temp list and so on) until I have no
more items in the queue.
// some notes to help you understand the code
PriorityQueue<Results> r, size(4), elementsEqualByTime(1=2,3=4);
List<Comments> c, size(2);
ArrayList temp;
if (c.size() != r.size() && resultIter.hasNext()) {
//first iteration will compare element 0 to itself -> 100% true
ResultObject r2 = resultIter.next();
ResultObject r1 = r2;
while (resultIter.hasNext() && r1.getTime().equals(r2.getTime())) {
temp.add(r1);
//we add the matching elements before we continue
r1 = r2;
temp.add(r1);
if (resultIter.hasNext()) {
//after we add the 2 matching elements we continue
r2 = resultIter.next();
}
}
//use the items in temp
temp.clear();
}
Right now it works for the 1st set of elements, but on the 2nd iteration
it adds no elements to the temp ArrayList. I'd appreciate help with this
solution, but am also open to different suggestions.
No comments:
Post a Comment