Suppose I have a Queue<String>
and I want to empty the current contents of the queue and do something with each element. Using a loop I could do something like:
while (true) {
String element = queue.poll();
if (element == null) {
return;
}
System.out.println(element);
}
This feels a bit ugly. Could I do this better with streams?
Note that there may be other threads accessing the queue at the same time, so relying on the size of the queue to know how many items to poll would be error prone.