How to customize to iterate in priority order

I have a class with PriorityQueue field inside:

public class MyClass<T>{
    Queue<T> queue = new PriorityQueue<>();

I want somehow get a stream from MyClass and use foreach and want the sequence behave in priority order of my PriorityQueue. The easiest way is to override stream() method:

public Stream stream() {

but this will not expose the queue element in priority order. So the question is: how to make foreach stream method behave like:


Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

You could use Stream::generate and Queue::poll method to get create a Stream with elements from PriorityQueue with keeping their order: @Override public Stream<T> stream() { return Stream.generate(queue::poll); } However this might be dangerous because Stream::generate will be invoking poll constantly so it is potentially an inifinite Stream. Therfore using Stream::limit with the queue size should be considered : @Override public Stream<T> stream() { return Stream.generate(queue::poll) .limit(queue.size()); } Or you could simply return sorted stream : @Override public Stream<T> stream() { return .sorted(comparator); } where comparator is your comparator. In Java 9 you could use Stream::takeWhile with predicate that rejects… Read more »