Skip to content

Commit 117ea7f

Browse files
committed
SI-9424 Clarify behavior of PriorityQueue toString
Clarified that PriorityQueue will not print in order and gave an example of a workaround if one needs it. Documentation change only; no tests.
1 parent 6426d0b commit 117ea7f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/library/scala/collection/mutable/PriorityQueue.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ import generic._
1818
*
1919
* Only the `dequeue` and `dequeueAll` methods will return methods in priority
2020
* order (while removing elements from the heap). Standard collection methods
21-
* including `drop` and `iterator` will remove or traverse the heap in whichever
22-
* order seems most convenient.
21+
* including `drop`, `iterator`, and `toString` will remove or traverse the heap
22+
* in whichever order seems most convenient.
23+
*
24+
* Therefore, printing a `PriorityQueue` will not reveal the priority order of
25+
* the elements, though the highest-priority element will be printed first. To
26+
* print the elements in order, one must duplicate the `PriorityQueue` (by using
27+
* `clone`, for instance) and then dequeue them:
28+
*
29+
* @example {{{
30+
* val pq = collection.mutable.PriorityQueue(1, 2, 5, 3, 7)
31+
* println(pq) // elements probably not in order
32+
* println(pq.clone.dequeueAll) // prints Vector(7, 5, 3, 2, 1)
33+
* }}}
2334
*
2435
* @tparam A type of the elements in this priority queue.
2536
* @param ord implicit ordering used to compare the elements of type `A`.

0 commit comments

Comments
 (0)