Why Does Priorityqueue.Tostring Return the Wrong Element Order

Why does PriorityQueue.toString return the wrong element order?

You need to poll the items from the PriorityQueue one by one. toString doesn't do that.

So instead of your System.out.println(queue); do this:

while(!queue.isEmpty()) {
System.out.println(queue.poll());
}

The reason is that the PriorityQueue is never completely sorted internally, lookup how a heap works for more detail. Polling items from it fixes the heap during the calls, thus it should output the elements in sorted order.

Why does toString of PriorityQueue returns elements out of order?

The order returned by the Iterator of a PriorityQueue is not guaranteed (emphasis theirs):

The Iterator provided in method iterator() [...] [is] not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

This is what toString() uses, therefore the order of the elements in the toString() output is also not specified

What is guaranteed is that multiple calls to poll() will return the values in the appropriate order:

while (!jumps.isEmpty()) {
System.out.println(jumps.poll());
}

Java PriorityQueue with Custom Objects not Sorting Correctly

You inserted them in priority queue. But then you need to poll the queue to get the right order of words.

        while (!maxHeap.isEmpty()) {
System.out.println(maxHeap.poll());
}

Also please note the order field won't get altered just because you inserted them in priority queue. It just shows the order in which the word appears in original sentence.

Write that loop after your for loop where you inserted. Then execute again. You will see right order.

Priority queue is not maintaining sorting order

In a priority queue, the only guarantee you have is that the head is the lowest (or greatest, depending on your comparison). The internal structure is not necessarily a sorted list. Actually, in Java, it's a heap:

PriorityQueue

An unbounded priority queue based on a priority heap.

However, if you do a loop that poll() the first item, then print it, again and again until the priority queue is empty. The elements should be printed from the lowest to the greatest element.

How to Sort PriorityQueue in Java with class

Your comparator lambda is correct, you can use it as-is. You just need to poll the queue in order to fetch the elements in the correct order:

    while (!pq.isEmpty()) {
Schedule s = pq.poll();
System.out.println("income:" + s.income + " " + "time: " + s.date);
}

PriorityQueue sorted but the two biggest

PriorityQueue only returns the lowest element from its head. It doesn't sort all the elements, so if you traverse the queue with pq.toString(), elements may not appear in order. This happens because, internally, PriorityQueue.toString() uses the PriorityQueue.iterator() method and, according to the docs:

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

If you want to print the elements of the priority queue in order, you should change this code:

System.out.println(pq.toString());

To the following:

while (!pq.isEmpty()) 
System.out.println(pq.remove());

How is the Java priority Queue supposed to work?

System.out.println is invoking the toString() method, which is using the iterator, which is not guaranteed to respect the natural ordering. From the docs: "The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order."

Maintaining a PriorityQueue of Pairs such that it's elements are sorted based on the two fields of the Pair class

Your priority queue has the elements in the right order already. You have been misled by the order it is printed - See PriorityQueue.toString wrong element order

If you repeatedly call poll in a loop, you can verify the same.



Related Topics



Leave a reply



Submit