• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Ordering elements in a PriorityQueue

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

This may have been asked before but I will ask again.

In my class I have a priority queue that orders its elements according to the specified comparator. However I do not fully understand why the elements are not ordered from the highest to lowest values as I expect, could somebody please explain. Here's my class


// Priority queuing
import java.util.*;

class PriorityQ {
static class SortComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return b.compareTo(a);
}
}

public static void main(String[] args) {
SortComparator sc = new SortComparator();
Integer[] sa = {7, 4, 1, 9, 5, 8, 6, 2, 3 };
PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>(9, sc);

for(Integer i: sa) {
pQueue.offer( i );
System.out.print(i + " ");
}
System.out.print("\n");

System.out.println(pQueue);

System.out.println("Peek " + pQueue.peek());
System.out.println("Size " + pQueue.size());

}
}



output:
7 4 1 9 5 8 6 2 3
[9, 7, 8, 4, 5, 1, 2, 3]
Peek 9
Size 9

 
Master Rancher
Posts: 4273
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this is normal. The toString() method is defined to show elements in the same order as the iterator, and the iterator for a PriorityQueue is defined to not be in any particular order. See here for more discussion. If you want the elements in priority order, use poll().
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Yes, this is normal. The toString() method is defined to show elements in the same order as the iterator, and the iterator for a PriorityQueue is defined to not be in any particular order. See here for more discussion. If you want the elements in priority order, use poll().


Right, a PriorityQueue only guarantees that elements will be removed in the correct order.
 
Matthew Aramah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic